如何使用`在斯波克测试
问题描述:
then`块我是新来的斯波克和通过的文件已经走了,但还是不完全了解如何使用then
部分。如果我想比较两个字符串将在then
区块中进行什么操作?如何使用`在斯波克测试
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
区块中,您必须检查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
你不必测试表达式。你的话应该是'string1 == string2' –
不,它应该是'String1!= String2',因为当它们相等时测试会失败。但是请不要使用以大写字母开头的变量名称,因为按照惯例,您只能像这样写类名称。顺便说一句,测试也可以是:'expect:'(linefeed)'!new File('/ path/to/file')。text.toLowerCase()。contains(“books”)''。这对于某些人来说可能不那么容易阅读(虽然对我来说更容易些),但是也一样,甚至表示文本文件**包含该单词,而不是完全等于它。我认为测试不应该比必要更冗长。 – kriegaex