[英]Grails unit testing controller setting up params
我有以下代码的控制器:
def profile = Profile.findByProfileURL(params.profileURL)
和这样的单元测试:
@TestMixin(GrailsUnitTestMixin)
@TestFor(ProfileController)
@Mock([User])
class ProfileControllerTests {
def void testIndex() {
mockDomain(User, [[firstname: 'Niko',...]])
controller.params.profileURL = 'niko-klansek'
controller.index()
...
}
}
当我运行测试时,控制器中出现以下异常:
No signature of method: sportboard.core.profile.Profile.methodMissing() is applicable for argument types: () values: []
因此,在控制器中看不到我在测试中设置的参数值profileURL? 如何为控制器设置参数,使其可见?
异常是神秘的,但它表示没有模拟您的Profile
域类。 您应该将其添加到@Mock
注释中。 另外, @TestMixin
可以省略@TestMixin,并且您不应该在测试中直接使用mockDomain
。 只需保存此用户实例。 总共应该看起来像这样:
@TestFor(ProfileController)
@Mock([User, Profile])
class ProfileControllerTests {
def void testIndex() {
def user = new User(firstName: 'Niko').save()
controller.params.profileURL = 'niko-klansek'
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.