testing - 如何在 spock 测试中使用 `then` block

标签 testing spock

我是 spock 的新手,已经阅读了文档,但仍然不完全理解如何使用 then 部分。如果我想比较两个字符串,then block 中会出现什么?

setup:
def String1 = "books"
def String2 =  new File('/path/to/file').text

when: 
String1 = String1.toLowerCase()
String2 = String2.toLowerCase()

then:
if (String1 == String2) {
    print "file contains the word" + String1
}

我需要测试在两个字符串相等但当前通过时失败。

最佳答案

可能你想这样做:

setup:
def string1 = "books"
def string2 = new File('/path/to/file').text

when:
string1 = string1.toLowerCase()
string2 = string2.toLowerCase()

then:
string1 != string2

但是您要检查这两个对象是否不相等。所以在 when block 中,你必须检查 equals 方法。所以你的测试应该是这样的:

setup:
def string1 = "books".toLowerCase()
def string2 = new File('/path/to/file').text.toLowerCase()

when:
boolean notEquals = string1 != string2

then:
notEquals

或更短:

setup:
def string1 = "books".toLowerCase()
def string2 = new File('/path/to/file').text.toLowerCase()

expect:
string1 != string2

关于testing - 如何在 spock 测试中使用 `then` block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42261281/

相关文章:

html - 从 Protractor 调用 Angular 函数

testing - 系统测试和端到端测试的区别

java - 用 spock 模拟的值不会转移到 Kotlin 吗?

java - 在方法中模拟新对象的创建

ruby-on-rails - Rails minitest 设计错误

reactjs - 为什么在我使用 React 测试库的测试中没有执行 jest 模拟函数?

grails - 为 UL LI 创建 Geb 模块

spring-boot - 有没有办法在使用 Spock 框架时使用 @MockBean 注释

angularjs - Protractor :测试引导警报

grails - Spock 集成测试用例中的应用真实路径问题