user2186466
user2186466

Reputation: 51

How to mock g.message while writing unit test for grails controller

I am using grails version 1.3.7. In my application in controller, I am making use of messages.properties and fetching the value of the property as

g.message(code:messageKey, args:msgParamsArr)

But when I started writing unit tests for the action in the controller, it gave me errors.

Please can you help me out to understand how to mock g.message exactly so that the existing code will fetch the message properties from messages.properties only.

Upvotes: 3

Views: 911

Answers (2)

Pete
Pete

Reputation: 1

This works like a champ in Grails 2.4.5:

controller.metaClass.message = { Map attrs -> attrs.toString() }

Upvotes: -1

Igor
Igor

Reputation: 33993

You can mock it with:

controller.metaClass.message = { message ->
    message.code
}

You can include message.args too if you want to just validate the arguments with .contains().

Upvotes: 3

Related Questions