简体   繁体   中英

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. That "MyObject" is just an orm class, and when I run that application everything seems to be working fine, but the test is failing.

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. So findWhere will return the object I already mocked, and pass it on to the service.

Any ideas why this is failing ?

对于模拟静态方法,您应该使用v0.7中引入的Spock的GroovyStub类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM