在Grails应用程序中测试自定义约束 [英] Testing custom constraints in Grails App
本文介绍了在Grails应用程序中测试自定义约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下作为我的单元测试:
I have the following as my unit test:
void testCreateDealer() {
mockForConstraintsTests(Dealer)
def _dealer= new Dealer( dealerName:"ABC",
Email:"abc-motors@global.com",
HeadOffice:"",
isBranch:false)
assertFalse _dealer.validate()
}
测试我得到以下错误:
No signature of method: static com.myCompany.Dealer.findByDealerNameIlike() is applicable for argument types: (java.lang.String) values: [ABC]
我的域类。如何测试此?
I use some custom constraints in my domain class. How Can I test this?
static constraints = {
dealerName(blank:false, validator:
{ val, obj ->
def similarDealer = Dealer.findByDealerNameIlike(val)
return !similarDealer || (obj.id == similarDealer.id)
}
)
推荐答案
尝试将mockForConstraintsTests()更改为mockDomain在约束中使用Dealer.findX()方法,这依赖于Dealer域。
Try changing mockForConstraintsTests() to mockDomain() - you're using a Dealer.findX() method in the constraint, which relies on the Dealer domain.
顺便说一下,测试仍然会失败,除非你创建了类似的经销商在测试类的setUp()方法中。
Incidentally, the test will still fail unless you've created a similar dealer in the setUp() method of the test class.
这篇关于在Grails应用程序中测试自定义约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文