【发布时间】:2013-10-01 20:02:53
【问题描述】:
代码如下。我想使用 obj.&method 来传递对它的引用。然而,当试图测试它时,嘲笑它是行不通的。测试中有什么我可以做的吗?
运行测试的结果是它抛出了“不应该到这里”的异常。
import grails.test.mixin.TestFor
@TestFor(SomeController)
class SomeControllerTest {
void testSomething() {
def control = mockFor(SomethingElse)
control.demand.someMethod(1) { int num, String str, Map another, List param ->
println 'worked'
}
controller.obj = control.createMock()
controller.underTest()
control.verify()
}
}
class SomeController {
SomethingElse obj
void underTest() {
otherCall(obj.&someMethod) // **
}
void otherCall(toRun) {
String result = toRun(1, 'blah', null, null) // ** doesn't call mock here
}
}
class SomethingElse {
String someMethod(int num, String str, Map another, List param) {
throw new RuntimeException('should not get here')
}
}
【问题讨论】:
标签: unit-testing grails groovy mocking