Search code examples
grailsinternationalizationintegration-testing

i18n in Grails Integration Test


Supposing I have a controller method that uses i18n to set flash.message

def someMethod(){
  // ...
  flash.message = message(code: 'label.generic.delete.ok.message', default: 'Delete OK')
  // ...
}

When I want to write an integration test to test this controller, I can't seem to find how to test the value of that message.

My problem is that I do not want to hard code the message in the test by doing something like this

assert controller.flash.message == "Delete successfull"

I'd rather do something like this

assert controller.flash.message == message(code: 'label.generic.delete.ok.message')

This throws a groovy.lang.missingMethodException.

I've looked at other answers here but the only solutions were given for unit tests.

I'm using grails 2.2.4.

Does anyone know how to access message() in integration tests?


Solution

  • You can test i18 messages in integration test as:

    inject the bean messageSource in test spec and call getMessage(String code, Object[] args, Locale locale)

    Here

    code: to lookup up, such as 'calculator.noRateSet' and

    args: Array of arguments that will be filled in for params within the message in your case it can be set as null and

    locale: the Locale in which to do the lookup e.g

    messageSource.getMessage('calculator.noRateSet', null, controller.request.getLocale())