Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created September 24, 2010 08:42
Show Gist options
  • Save robfletcher/595061 to your computer and use it in GitHub Desktop.
Save robfletcher/595061 to your computer and use it in GitHub Desktop.
class GreetingController {
def index = {
[message: message(code: "greeting.message", args: [params.name])]
}
}
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