rastko
rastko

Reputation: 655

Grails unit testing controller setting up params

I have controller with following code:

def profile = Profile.findByProfileURL(params.profileURL)

and unit test like this:

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

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

        controller.params.profileURL = 'niko-klansek'

        controller.index()

        ...
    }

}

When I run the test I get following exception in the controller referring to :

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

So params value profileURL that I have set in the test is not visible from the controller? How can I set params for controller so it is visible?

Upvotes: 1

Views: 4845

Answers (1)

Tomasz Kalkosiński
Tomasz Kalkosiński

Reputation: 3723

Exception is cryptic, but it says that your Profile domain class is not mocked. You should add it to @Mock annotation. Also, @TestMixin can be ommited here and you shouldn't use mockDomain directly in test. Just save this user instance. Altogether it should look like this:

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

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

Upvotes: 1

Related Questions