Unit testing Grails单元测试控制器设置参数

Unit testing Grails单元测试控制器设置参数,unit-testing,grails,params,Unit Testing,Grails,Params,我有以下代码的控制器: def profile = Profile.findByProfileURL(params.profileURL) 单元测试如下: @TestMixin(GrailsUnitTestMixin) @TestFor(ProfileController) @Mock([User]) class ProfileControllerTests { def void testIndex() { mockDomain(User, [[firstname:

我有以下代码的控制器:

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()

        ...
    }

}
@TestFor(ProfileController)
@Mock([User, Profile])
class ProfileControllerTests {
    def void testIndex() {
        def user = new User(firstName: 'Niko').save()

        controller.params.profileURL = 'niko-klansek'
        ...
    }
}
当我运行测试时,控制器中出现以下异常,涉及:

No signature of method: sportboard.core.profile.Profile.methodMissing() is applicable for argument types: () values: []

那么我在测试中设置的params value profileURL在控制器中不可见?如何为控制器设置参数,使其可见?

异常是神秘的,但它表示您的
配置文件
域类未被模拟。您应该将其添加到
@Mock
注释中。另外,
@TestMixin
可以在这里使用,您不应该在测试中直接使用
mockDomain
。只需保存此用户实例。总之,它应该是这样的:

@TestMixin(GrailsUnitTestMixin)
@TestFor(ProfileController)
@Mock([User])
class ProfileControllerTests {

    def void testIndex() {
        mockDomain(User, [[firstname: 'Niko',...]])

        controller.params.profileURL = 'niko-klansek'

        controller.index()

        ...
    }

}
@TestFor(ProfileController)
@Mock([User, Profile])
class ProfileControllerTests {
    def void testIndex() {
        def user = new User(firstName: 'Niko').save()

        controller.params.profileURL = 'niko-klansek'
        ...
    }
}

异常是神秘的,但它表示您的
概要文件
域类并没有被模拟。您应该将其添加到
@Mock
注释中。另外,
@TestMixin
可以在这里使用,您不应该在测试中直接使用
mockDomain
。只需保存此用户实例。总之,它应该是这样的:

@TestMixin(GrailsUnitTestMixin)
@TestFor(ProfileController)
@Mock([User])
class ProfileControllerTests {

    def void testIndex() {
        mockDomain(User, [[firstname: 'Niko',...]])

        controller.params.profileURL = 'niko-klansek'

        controller.index()

        ...
    }

}
@TestFor(ProfileController)
@Mock([User, Profile])
class ProfileControllerTests {
    def void testIndex() {
        def user = new User(firstName: 'Niko').save()

        controller.params.profileURL = 'niko-klansek'
        ...
    }
}

问题是Profile是一个抽象类,然后我得到:'不能实例化bean类[sportboard.core.Profile.Profile]:它是一个抽象类吗?'所以模拟类扩展了抽象Profile类。您可以在此处找到示例解决方法:。确切地说,用户扩展了配置文件。谢谢你的链接!虽然我写过你不应该直接使用mockDomain,但是在测试的第一行试试mockDomain(Profile):)问题是Profile是一个抽象类,然后我得到:“不能实例化bean类[sportboard.core.Profile.Profile]:它是一个抽象类吗?”所以mock类扩展了你的抽象Profile类。您可以在此处找到示例解决方法:。确切地说,用户扩展了配置文件。谢谢你的链接!尽管我写过不应该直接使用mockDomain,但请在测试的第一行尝试mockDomain(概要文件:)