g:message
tag. However, because tag access is magic wired up by Grails it's not available in unit tests without some effort.This is a perennial nuisance. Not exactly difficult to solve and yet something I find myself solving over and over in different tests and different projects.
I've come up with what I think is a pretty good and re-usable solution. It allows you to specify messages if you want to, or just use the message code if it's not something you care about in a particular test. As an aside, at a unit test level, I think testing that the correct message code is being used is probably the right thing to do; I'd leave testing actual message text to end-to-end tests.
Here's an example. Imagine we're testing the following controller that displays a simple greeting:
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])] | |
} | |
} |
Here's a spec that shows the behaviour both when a message is specified and when it isn't:
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) | |
} | |
} | |
} |
A few things to note:
- The stubbed message tag returns the code if there is no message defined
- message arguments are ignored unless there is a message defined
Although in the example I've used Spock, this technique would work equally well with JUnit tests extending ControllerUnitTestCase. It will also work just as well for tag libs tests extending TagLibUnitTestCase or TagLibSpec.
11 comments:
Thank you for another fantastic posting. Where else could anyone get that kind of information in such a perfect way of writing? I have a speech next week, and I was looking for more info.
giochi di winx
As nice blog,but you could have made this blog more cool online essay writer if you could configure the language changer pluigin but all the same you have done a good job
Beautiful Dresses 2016
Pakistani Dresses
Dresses
Gull Ahmed Collection
I love it whenever people come together and share thoughts. Great blog, stick with it!
http://www.menstopspot.com | http://www.pureitfresh.com | http://www.warmmensfashion.com | http://www.urbanplussizeclothing.org | http://www.burgundybodycondress.com
Thanks for your sharing! The information your share is very useful to me and many people are looking for them just like me! thank you! I hope you have many useful articles to share with everyone!
happy wheels
We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work. Friv1 Friv 2 Friv7 Friv20
Thanks for your post! Through your pen I found the problem up interesting! I believe there are many other people who are interested in them just like me! Thanks your shared!... I hope you will continue to have similar posts to share with everyone! I believe a lot of people will be surprised to read this article!abc ya
Real useful and important. Good luck! clits
merriage anniversary wishes for husband
high quality designer replica i40 o4l93o5c44 replica louis vuitton bags i93 u6u49y0h30 Ysl replica bags j21 l6i26f4h32
Post a Comment