Grails 2.1单元测试命令对象mockForConstraintsTests不工作? [英] Grails 2.1 Unit Testing Command Object mockForConstraintsTests not working?

查看:144
本文介绍了Grails 2.1单元测试命令对象mockForConstraintsTests不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已对此命令对象使用手动写入以及Grails生成的单元测试:

 包myapp 

@ grails.validation.Validateable
类SearchCommand {
String basisBuild
String buildToSearch

static constraints = {
basisBuild )
}
}

在我手写单元测试失败后, Grails:

  create-unit-test myapp.SearchCommand 

我填写单元测试,并对每个关于模拟约束的文档做出应该通过的断言:

  package myapp 
import static org.junit.Assert。*

import grails.test.mixin。*
import grails.test.mixin.support。*
import org.junit。*

@TestMixin(GrailsUnitTestMixin)
类SearchCommandTests {

void setUp ){
mockForConstraintsTests(SearchCommand)
}

void tearDown(){
//拖拽逻辑
}
$ b b void testSomething(){
SearchCommand commandUnderTest = new SearchCommand()

commandUnderTest.validate(basisBuild:)

assertEqualsblank,commandUnderTest.errors ['basisBuild']
}
}

遇到此失败?

  grails> test-app 
|运行9个单元测试... 9/9
|失败:testSomething(com.siemens.soarian.sf.gap.SearchCommandTests)
| java.lang.AssertionError:expected:< blank>但是:< null>
at org.junit.Assert.fail(Assert.java:93)


方案

我相信我发现grails支持在grails 2.0中单元测试命令对象的方式。



//www.jworks.nl/2012/04/12/testing-command-objects-in-grails-2-0/\">http://www.jworks.nl/2012/04/12/testing-command- objects-in-grails-2-0 /


I have used manually written as well as Grails generated Unit tests for this command object:

   package myapp

    @grails.validation.Validateable
    class SearchCommand {
       String basisBuild
       String buildToSearch

       static constraints = {
          basisBuild(blank: false)
       }
    }

After having my hand written unit test fail I used Grails:

create-unit-test  myapp.SearchCommand

I filled in the Unit Test, and made an assertion that should pass per documentation on mocked constraints:

package myapp
import static org.junit.Assert.*

import grails.test.mixin.*
import grails.test.mixin.support.*
import org.junit.*

@TestMixin(GrailsUnitTestMixin)
class SearchCommandTests {

    void setUp() {
        mockForConstraintsTests(SearchCommand)
    }

    void tearDown() {
        // Tear down logic here
    }

    void testSomething() {
        SearchCommand commandUnderTest = new SearchCommand()

        commandUnderTest.validate(basisBuild: "")

        assertEquals "blank", commandUnderTest.errors['basisBuild']
    }
}

Why am I getting this failure?

grails> test-app
| Running 9 unit tests... 9 of 9
| Failure:  testSomething(com.siemens.soarian.sf.gap.SearchCommandTests)
|  java.lang.AssertionError: expected:<blank> but was:<null>
        at org.junit.Assert.fail(Assert.java:93)

解决方案

I believe I found the grails supported way to unit test Command objects in grails 2.0. You need to use mockCommandObject provided by the ControllerUnitTestMixin.

Credit to Erik

http://www.jworks.nl/2012/04/12/testing-command-objects-in-grails-2-0/

这篇关于Grails 2.1单元测试命令对象mockForConstraintsTests不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆