[英]Failing Unit Test In Grails 1.3.7 Spock
I have this piece of code in a controller: 我在控制器中有这段代码:
def update = {
Map model = [:]
model.foo = params.foo
model.bar = params.bar
def result = ""
MyObject obj = MyObject.findWhere(bar:bar, foo:foo)
MyObjectService.updateObj(model,obj)
result = true
render result as JSON
}
And this simple unit test: 这个简单的单元测试:
def 'controller update'() {
given:
controller.params.foo = foo
controller.params.bar = bar
MyObject obj = new MyObject(bar:bar, foo:foo)
mockDomain(MyObject,[obj])
when:
controller.update()
then:
1 * MyObject.findWhere(bar:bar, foo:foo) >> obj
1 * MyObjectService.updateObj(model,obj)
and:
def model = JSON.parse(controller.response.contentAsString)
model == true
where:
foo = "0"
bar = "1"
}
Now this is failing by and it is telling me that, "not static method findWhere is applicable..." for those arguments. 现在这失败了,它告诉我,这些参数“不是静态方法findWhere适用...”。 That "MyObject" is just an orm class, and when I run that application everything seems to be working fine, but the test is failing. “ MyObject”只是一个orm类,当我运行该应用程序时,一切似乎都正常,但测试失败了。
My logic is this: 我的逻辑是这样的:
I want to count how many times the findWhere
and updateObj
methods are call and I am also mocking their response. 我想计算一下findWhere
和updateObj
方法被调用的次数,而且我还在嘲笑它们的响应。 So findWhere
will return the object I already mocked, and pass it on to the service. 所以findWhere
将返回我已经findWhere
的对象,并将其传递给服务。
Any ideas why this is failing ? 有什么想法为什么会失败吗?
对于模拟静态方法,您应该使用v0.7中引入的Spock的GroovyStub类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.