gpt4 book ai didi

grails - Controller 单元测试中的模拟gorm方法

转载 作者:行者123 更新时间:2023-12-02 14:30:45 25 4
gpt4 key购买 nike

我正在使用Grails 2.4.4,并尝试为 Controller 编写Spock测试,并在尝试模拟gorm查找器时遇到问题。文档似乎有点混乱。据说它有可能完全模仿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{
....

现在我进行了测试(在unit文件夹中,即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
}

}

最佳答案

Grails测试Mixins的方便功能是它们提供了Gorm的轻量级内存实现,因此您实际上不必对Gorm进行任何调用(就像您在rails中使用rspec一样)-被测试的代码可以没有看到区别。

因此,在本规范中,只需声明 Controller 的响应即可。

given: def alert = new Alert(..)

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

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

关于grails - Controller 单元测试中的模拟gorm方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com