Spock rightShift(模拟)操作员显然无法正常工作
Spock rightShift (mocking) operator apparently not working
这是我的 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.attributes["favcolor"] >> 'red'
fizzBuzz.attributes["age"] >> '36'
Something something = new Something(fizzBuzz)
when:
Whoah whoah = something.doSomething()
then:
!whoah.things
}
这里是 FizzBuzz
模拟:
public interface FizzBuzz extends Serializable {
Map<String, Object> getAttributes();
}
当我 运行 我得到:
java.lang.NullPointerException: Cannot invoke method rightShift() on null object
at com.commercehub.houston.SomethingSpec.when favorite color is red then doSomething produces empty list of things fail(SomethingSpec.groovy:18)
Process finished with exit code 255
它在第 18 行中引用的 'null object' 是 fizzBuzz
或其 attributes
映射。 为什么?
您正在尝试使用多级间接寻址,>>
正应用于 .attributes["favcolor"]
的结果,该结果为空(因为 .attributes
是一个空映射).相反,只需初始化地图:
fizzBuzz.attributes >> [favcolor: 'red', age: 36]
(另外,您的意思是 age
是一个字符串吗?)
就我而言,我意识到我不小心声明了 when
块的结果。
when:
Whoah whoah = something.doSomething() >> expectedResult
这是我的 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.attributes["favcolor"] >> 'red'
fizzBuzz.attributes["age"] >> '36'
Something something = new Something(fizzBuzz)
when:
Whoah whoah = something.doSomething()
then:
!whoah.things
}
这里是 FizzBuzz
模拟:
public interface FizzBuzz extends Serializable {
Map<String, Object> getAttributes();
}
当我 运行 我得到:
java.lang.NullPointerException: Cannot invoke method rightShift() on null object
at com.commercehub.houston.SomethingSpec.when favorite color is red then doSomething produces empty list of things fail(SomethingSpec.groovy:18)
Process finished with exit code 255
它在第 18 行中引用的 'null object' 是 fizzBuzz
或其 attributes
映射。 为什么?
您正在尝试使用多级间接寻址,>>
正应用于 .attributes["favcolor"]
的结果,该结果为空(因为 .attributes
是一个空映射).相反,只需初始化地图:
fizzBuzz.attributes >> [favcolor: 'red', age: 36]
(另外,您的意思是 age
是一个字符串吗?)
就我而言,我意识到我不小心声明了 when
块的结果。
when:
Whoah whoah = something.doSomething() >> expectedResult