0

以下是我们正在做的事情,它在 grails 2.0.0 和 spock 0.6 中工作

一个简单的域:

package com.grails
class User {
    String name, password
    def methodWithNoArguments(){
        return "Object: methodWithNoArguments"
    }
    static constraints = {
    }
}

单位规格:

package com.grails

import spock.lang.* 
import grails.test.mixin.TestFor 
import grails.test.mixin.Mock 
import grails.test.GrailsMock

@TestFor(UserController)  // testing for 
@Mock([User])             // collaborators class 
UserControllerSpec extends  Specification {
    // DOES NOT work in 2.2.3 spock 0.7,
    // WORKS in grails-2.0.0 spock 0.6
    def 'create a Domain collaborator using Mock'() {
        given: 'user'
        // can't mock this anymore, causes classcast exception.
        User user = Mock(User)

        user.methodWithNoArguments()  >> { "Mock: (3) methodWithNoArguments" }

        when: 'we call the method'
        def val1 = user.methodWithNoArguments()

        then: 'value should be from the mocked method'
        val1 == "Mock: (3) methodWithNoArguments"
    } }

以上在 grails 2.2.3 和 spock 0.7 中导致以下异常。注意(它适用于 grails-2.0.0 和 spock 0.6)

| java.lang.ClassCastException:com.grails.User 无法在 org.spockframework.mock.runtime.ProxyBasedMockFactory$CglibMockFactory.createMock(ProxyBasedMockFactory.java:93) 的 org.spockframework 中转换为 net.sf.cglib.proxy.Factory。 mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:49) at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:51) at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java: 44) 在 org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:282) 在 org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java) 的 org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:47) :99) 在 com.grails.UserControllerSpec。

想知道是否可能存在一些不兼容的库,这里是 BuildConfig.groovy

dependencies {
    test "org.spockframework:spock-grails-support:0.7-groovy-2.0"
}

plugins {
    runtime ":hibernate:$grailsVersion"
    runtime ":jquery:1.8.3"
    runtime ":resources:1.2"

    build ":tomcat:$grailsVersion"

    runtime ":database-migration:1.3.2"

    compile ':cache:1.0.1'
    test(":spock:0.7") {
        exclude "spock-grails-support"
    }
}

示例测试应用程序位于 git@github.com:nbostech/grails-spock-2.2.3.git

git checkout grails-2.0.0; <2.0.0>/bin/grails test-app --unit UserControllerSpec; //所有测试通过

git结帐大师;<2.2.3>/bin/grails test-app --unit UserControllerSpec; // 一项测试失败..

4

1 回答 1

2

Grails 和 Spock 模拟不打算以这种方式混合。要么让它成为 Grails @Mock,要么成为 Spock Mock()

于 2013-06-29T17:24:21.183 回答