使用Spock在Grails域类中存根Gorm和其他方法

时间:2013-03-21 10:16:08

标签: grails mocking gorm stub spock

很抱歉,如果这是一个新手问题,但我非常感谢社区可以提供的任何有关我在Grails服务,LocationService中存储以下方法的问题的见解。

Location locate(String target, String locator, Application app, boolean sync = true) {
    if (!target) throw new IllegalArgumentException("Illegal value for msid: " + target)
    def locRequest = Request.create(target, Type.LOCATE) 
    if (!locRequest.save()) {
            return Location.error(target, "Error persisting location request")
    }
    locationSource.locateTarget(target, locator, app, sync)
}

我有一个域类,Request,以及默认的GORM方法也有一些额外的域方法,例如。

下面的create()方法
@EqualsAndHashCode
class Request {

    String reference
    String msid
    Type type
    Status status
    Destination destination
    DateTime dateCreated
    DateTime dateCompleted

    static create(String msid, Type type, Destination destination = Destination.DEFAULT) {
            new Request(reference: reference(type), type: type, status: Status.INITIATED, dateCreated: new DateTime())
    }

最后,我有一个Spock规范。我需要模拟默认的GORM方法,但也需要一些额外的域逻辑,例如静态创建方法,以便返回一个有效的对象,以便在被测试的代码中保留。

理想情况下,我会使用Spock模拟但我不能在这里使用它们,因为根据Peter N的下面的帖子,他们需要注入调用者,在这种情况下请求(我试图模拟) ,在LocationService中的locate方法中创建为局部变量:

https://groups.google.com/forum/?fromgroups=#!topic/spockframework/JemiKvUiBdo

我也不能使用Grails 2.x @Mock注释,虽然这会模拟GORM方法,但我不确定我是否可以从Request类中模拟/存储额外的静态create()方法。

因此,最后,我一直在尝试使用Groovy StubFor / MockFor方法来执行此操作,因为我相信这些将通过将其包装在use闭包中来用于对测试方法的调用(如下所示)。

以下是测试规范:

@TestFor(LocationService)
// @Mock(Request)
class LocationServiceSpec extends Specification {

    @Shared app = "TEST_APP"
    @Shared target = "123"
    @Shared locator = "999"

    def locationService = new LocationService()
    LocationSource locationSource = Mock()


  def "locating a valid target should default to locating a target synchronously"() {
      given:
            def stub = new StubFor(Request)
            stub.demand.create { target, type -> new Request(msid: target, type: type) }
            stub.demand.save { true }
            1 * locationSource.locateTarget(target, locator, app, SYNC) >> { Location.create(target, point, cellId, lac) }
            def location
      when: 
            stub.use {
                location = locationService.locate(target, locator, app)
            }
      then: 
            location
 }

但是,当我运行测试时,虽然stubbed create方法返回我的Request stub对象,但我在stubbed save方法上失败了:

groovy.lang.MissingMethodException: No signature of method:       com.domain.Request.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), last(), any()

任何人都可以指出我在这里做错了什么或建议解决我的特定情况的最佳方法,如果需要存根其他方法以及域类的GORM方法,我不能直接注入到代码中测试

提前谢谢你,

帕特里克

1 个答案:

答案 0 :(得分:8)

我相信您应该能够像使用GORM方法一样使用Grails'@Mock注释,然后您需要手动模拟静态方法:

@TestFor(LocationService)
@Mock(Request)// This will mock the GORM methods, as you suggested
class LocationServiceSpec extends Specification {
...
    void setup() {
        Request.metaClass.static.create = { String msid, Type type, Destination destination = Destination.DEFAULT ->
             //Some logic here
        }
    }
...

使用@Mock注释时,Grails将模拟默认方法(保存/获取/动态查找器),但它不会对您添加的任何其他方法执行任何操作,因此您需要手动模拟那些。