我已经为这个命令对象使用了手动编写和 Grails 生成的单元测试:
package myapp
@grails.validation.Validateable
class SearchCommand {
String basisBuild
String buildToSearch
static constraints = {
basisBuild(blank: false)
}
}
在我手写的单元测试失败后,我使用了 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)
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']
}
}
为什么我会失败?
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)
最佳答案
我相信我在 grails 2.0 中找到了 grails 支持的单元测试命令对象的方法。您需要使用 ControllerUnitTestMixin 提供的 mockCommandObject。
感谢埃里克
http://www.jworks.nl/2012/04/12/testing-command-objects-in-grails-2-0/
关于unit-testing - Grails 2.1 单元测试命令对象 mockForConstraintsTests 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16108556/