我是 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/