ry1*_*633 10 grails integration-testing
自从我4个月前开始工作以来,我对Grails完全不熟悉并且正在测试它的功能.几个星期前训练我测试的人离开了我们小组,现在我自己进行测试.我已经放缓已经发现的是,我就怎么做的Grails集成测试训练方式几乎是从我见过的人做的论坛和支持团体的方式(一个或多个)完全不同.我真的可以使用哪种方式是正确/最好的.我目前正在使用Grails 2.4.0,顺便说一句.
以下是我接受过培训的样式的集成测试示例模型.这是我应该做的正确甚至是最好的方式吗?
@Test
void "test a method in a controller"() {
def fc = new FooController() // 1. Create controller
fc.springSecurityService = [principal: [username: 'somebody']] // 2. Setup Inputs
fc.params.id = '1122'
fc.create() // 3. Call the method being tested
assertEquals "User Not Found", fc.flash.errorMessage // 4. Make assertions on what was supposed to happen
assertEquals "/", fc.response.redirectUrl
}
Run Code Online (Sandbox Code Playgroud)
dma*_*tro 18
由于使用了Grails 2.4.0,因此您可以利用默认情况下使用spock框架的好处.
Here 是样本单元测试用例,您可以在编写集成规范后进行建模.
注意:
test/integrationIntegrationSpec.@TestFor与单位规格相比,不使用.def myService在课堂级别将按规范注入服务.以上规格应如下:
import grails.test.spock.IntegrationSpec
class FooControllerSpec extends IntegrationSpec {
void "test a method in a controller"() {
given: 'Foo Controller'
def fc = new FooController()
and: 'with authorized user'
fc.springSecurityService = [principal: [username: 'somebody']]
and: 'with request parameter set'
fc.params.id = '1122'
when: 'create is called'
fc.create()
then: 'check redirect url and error message'
fc.flash.errorMessage == "User Not Found"
fc.response.redirectUrl == "/"
}
}
Run Code Online (Sandbox Code Playgroud)