When we mock or stub methods we can use the method arguments passed to the method in the response for the mocked or stubbed method. We must write a closure after the rightShift operator (>>
) and the closure arguments will resemble the arguments of the mocked or stubbed method. Alternatively we can use a single non-typed argument in the closure and this will contains the method argument list.
Let's create a specification where we use this feature. In the following sample we use a mock for the AgeService
used in the class under test. The method allowedMaxTime()
is invoked by the class under test and basically should return the maximum hour of the day a show can be broadcasted. In our specification we use the name of the show to return different values during the test.
Continue reading →
NOTE: This blog post is originally written for AngularJS 1.2.x; in 1.3.x the "input not showing invalid model values" has been fixed.
Although 1.3.x still has the "inconsistencies in how AngularJS parses data entry" the solution from this blog post isn't working for 1.3.x but I will try to find a fix for this within the next few weeks.
A while ago I noticed that AngularJS doesn't show invalid model values bound to an <input/>
There is also an open bug report about this: issue #1412 - input not showing invalid model values The bug can be easily illustrated through the following example:
Continue reading →
In a previous blog post we learned how we can unit test a template or view independently. But what if we want to unit test a controller that uses the render()
method and a template with the template
key instead of a view? Normally the view and model are stored in the modelAndView
property of the response. We can even use shortcuts in our test code like view
and model
to check the result. But a render()
method invocation with a template
key will simply execute the template (also in test code) and the result is put in the response. With the text
property of the response we can check the result.
In the following sample controller we use the header
template and pass a username
model property to render output.
Continue reading →
Since Grails 2 we can render binary output with the render()
method and the file
attribute. The file
attribute can be assigned a byte[]
, File
, InputStream
or String
value. Grails will try to determine the content type for files, but we can also use the contentType
attribute to set the content type.
In the following controller we find an image in our application using grailsResourceLocator
. Then we use the render()
method and the file
and contenType
attributes to render the image in a browser:
Continue reading →
Grails uses Spring and we can piggyback on the Spring support for resource loading to find for examples files in the classpath of our application. We can use the Spring org.springframework.core.io.Resource
or org.springframework.core.io.ResourceLoader
interface to find resources in our application.
And since Grails 2.0 we can also use the org.codehaus.groovy.grails.core.io.ResourceLocator
interface. In our code we can use the grailsResourceLocator
service which implements the ResourceLocator
interface. We must inject the grailsResourceLocator
service into our code and we use the method findResourceForURI(String)
to find a resource. The advantage of the grailsResourceLocator
service is that it knows about a Grails application. For example resources in plugins can also be accessed.
Continue reading →