Created
September 24, 2010 08:42
-
-
Save robfletcher/595061 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GreetingController { | |
def index = { | |
[message: message(code: "greeting.message", args: [params.name])] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import grails.plugin.spock.* | |
import org.springframework.context.* | |
import org.springframework.context.support.* | |
import spock.lang.* | |
class GreetingControllerSpec extends ControllerSpec { | |
@Shared def messageSource = new StaticMessageSource() | |
@Shared def pirateEnglish = new Locale("en", "BV") | |
def setupSpec() { | |
messageSource.useCodeAsDefaultMessage = true | |
messageSource.addMessage "greeting.message", pirateEnglish, "Ahoy there {0}!" | |
} | |
def setup() { | |
mockMessageTag(controller, messageSource) | |
} | |
@Unroll | |
def "greeting is rendered by index action"() { | |
given: | |
if (name) controller.params.name = name | |
if (locale) controller.request.addPreferredLocale(locale) | |
expect: | |
controller.index() == [message: message] | |
where: | |
name | locale | message | |
null | null | "greeting.message" | |
"Rob" | null | "greeting.message" | |
"Rob" | pirateEnglish | "Ahoy there Rob!" | |
} | |
// in reality this would be static imported from a helper class | |
static void mockMessageTag(artefact, MessageSource messageSource) { | |
artefact.metaClass.message = { attrs -> | |
messageSource.getMessage(attrs.code, attrs.args as Object[], delegate.request.locale) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment