Java 斯波克右移(模拟)操作员显然不工作
下面是我的Spock单元测试:Java 斯波克右移(模拟)操作员显然不工作,java,unit-testing,groovy,spock,Java,Unit Testing,Groovy,Spock,下面是我的Spock单元测试: def "when favorite color is red then doSomething produces empty list of things"() { given: FizzBuzz fizzBuzz = Mock(FizzBuzz) fizzBuzz.name >> 'Dark Helmet' fizzBuzz.attributes >> [:] fizzBuzz.attribute
def "when favorite color is red then doSomething produces empty list of things"() {
given:
FizzBuzz fizzBuzz = Mock(FizzBuzz)
fizzBuzz.name >> 'Dark Helmet'
fizzBuzz.attributes >> [:]
fizzBuzz.attributes["favcolor"] >> 'red'
fizzBuzz.attributes["age"] >> '36'
Something something = new Something(fizzBuzz)
when:
Whoah whoah = something.doSomething()
then:
!whoah.things
}
下面是FizzBuzz
mock:
public interface FizzBuzz extends Serializable {
Map<String, Object> getAttributes();
}
第18行所指的“空对象”是
fizzBuzz
或其属性
映射为什么?您试图使用多级间接寻址,
将应用于.attributes[“favcolor”]
的结果,该结果为空(因为.attributes
是空映射)。相反,只需初始化映射:
fizzBuzz.attributes >> [favcolor: 'red', age: 36]
(还有,你真的想把
age
变成一个字符串吗?在我的例子中,我意识到我在块时意外地声明了的结果
when:
Whoah whoah = something.doSomething() >> expectedResult
when:
Whoah whoah = something.doSomething() >> expectedResult