kma*_*man 4 testing grails spock
我一直在看grails控制器方法的一些奇怪的行为,这让我想要创建一个测试用例,看看我是否可以一致地重现这个.所以,我将首先解释这个问题,以便更好地理解我的问题.
我最近有一些奇怪的行为,当调用grails方法时,它有时无法完成所有方法实现(基本上所有代码都转到服务器调用)并且过早地重定向.我可以通过我的服务方法仍然运行来看到这个,但是ui已经(有时也错误地)重定向错误.我已经尝试通过它调试,但它也引导我到相同的地方,..我将在一个服务方法,但看到已经在ui上呈现的页面.如果我能得到一个好的测试用例,我觉得这可能更容易调试.
示例控制器代码:
def blam() {
Foo foo = Foo.get(params.id)
String msg = service.method1(foo)
service.method2(foo)
service.doSomething(foo)
redirect(action:"list", controller: "blam", params: [msg:msg, *:params])
}
Run Code Online (Sandbox Code Playgroud)
示例测试代码:下面的问题是它根本没有实际测试一个调用顺序,我想测试它是否按正确的顺序调用方法.我发现有几个例子说你可以调用多个声明,但是如果我移动方法并重新运行测试,我就不会看到差别.
def "test service call order and redirect"(){
when:
controller.actionMethod()
then:
service.method1(_) * 1
service.method2(_) * 1
service.doSomething(_) * 1
then:
response.redirectedUrl == "bam/list"
}
Run Code Online (Sandbox Code Playgroud)
任何有关如何测试此场景的帮助将不胜感激!另外我知道这不是主要问题,但欢迎重定向的想法!(或者我应该创建一个不同的问题?)
干杯!
使用多个then块:
then:
service.method1(_) * 1
then:
service.method2(_) * 1
then:
service.doSomething(_) * 1
Run Code Online (Sandbox Code Playgroud)
该订单应该得到尊重(参见http://spock-framework.readthedocs.org/en/latest/interaction_based_testing.html#invocation-order)
| 归档时间: |
|
| 查看次数: |
1268 次 |
| 最近记录: |