在控制器单元测试中模拟gorm方法

时间:2015-08-10 13:13:11

标签: grails gorm spock

我正在使用Grails 2.4.4并尝试为控制器编写Spock测试并在尝试模拟gorm finders时遇到问题。文档似乎有点令人困惑。据说它有可能完全模仿gorm,但我正在与它斗争..

继承人控制器

class AlertController {

SpringSecurityService springSecurityService


def dismiss() {

    Long id = request.getJSON().id

    log.debug ("dismiss called for alert id ${id}")

    Alert alert = Alert.get(id)

    if(alert){ 

        alert.acknowledged = true



        if(!alert.save(flush:true)){
            log.warn("dismiss failed: ${alert.errors}")
            def result = [status:1]
            render result as JSON
            return
        }else{
        ....

现在我接受了测试(在单元文件夹中,即NOT集成):

@TestFor(AlertController)
@Mock(Alert)
@TestMixin(DomainClassUnitTestMixin)
class AlertControllerSpec extends Specification {

def setupSpec() {

    new Alert(
            id: 1, acknowledged: false,alertDate: new Date(),comments: 'asas',dateCreated: new Date(), lastUpdated: new Date(),
            assignedTo: new User(id: 1,name: 'sdsd', email: 'asdasda@asdasd.com',firstName: 'aasdasdasd', lastName: 'asdasdasdasd',
                    trust: new Trust(id: 1))
    ).save(flush: true) //fails with Method on class [Alert] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.



}

def cleanup() {
}

void "test dismiss"() {

    when:

    // whats the correct way to mock the Alert.get(2)

    // and the correct way to mock save()  ??

    /*def mockControl = new GrailsMock(Alert)
    mockControl.demand.save() {-> return true}           // Instance method
    mockControl.demand.static.get() {id -> return new Alert()}  // Static method*/


    /*GrailsMock mockAlert = mockFor(Alert)
    mockAlert.demand.static.get() { Long id -> new Alert(id:1) }
    mockAlert.demand.static.save(flush: true) { -> true }
    mockAlert.createMock()*/

    controller.request.addHeader("Accept","application/json")
    request.json = "{'id':1}"
    controller.dismiss()

    then:
    response.json.status == '0'
}

}

所以我让测试工作,但我很想知道如何绕过保存所有实体...即只是模拟Alert.get()和Alert.save()动态调用

@TestFor(AlertController)
@Mock([Alert, User, Trust, Patient])
@TestMixin(DomainClassUnitTestMixin)
class AlertControllerSpec extends Specification {

def setupSpec() {

}

def cleanup() {
}


void testDismiss() {

    given:

    def user = new User(id: 1,name: 'sdsd', email:    'asdasda@asdasd.com',firstName: 'aasdasdasd', lastName: 'asdasdasdasd',
            trust: new Trust(version:1, name:'asasas',shortname: 'sdsdsd' ).save(failOnError: true)).save(failOnError: true)

    def alert = new Alert(
            id:1,type:'sdfsdf', status:'sdsdds',acknowledged: false,alertDate: new Date(),comments: 'asas',dateCreated: new Date(),     lastUpdated: new Date(),
            assignedTo: user,
            patient: new Patient(

            )).save(failOnError: true)



    when:
    controller.request.addHeader("Accept","application/json")
    controller.request.json = [id:1]
    controller.dismiss()

    then:
    response.json.status == 0
}

}

1 个答案:

答案 0 :(得分:2)

The handy feature of Grails testing Mixins is that they provide a lightweight in-memory implementation of Gorm, so you do not actually have to stub any calls to Gorm (like you would for example with rspec in rails) - the code under test just does not see the difference.

So, in this spec just assert the response of the controller.

given: def alert = new Alert(..)

when:
controller.request.json = [id: alert.id]
controller.action()

then: controller.response.json == [..]