I want to test a method of a class “Service”

  1. public void AddDrawing(Converter conv, Drawing drw) {
  2.    []
  3.    conv.AddDrawing(drw);
  4.    []
  5. }

The problem is, that Converter provides no way to determine which drawings have been added. So I tried the following, like I would have done in Java – subclass the Converter and add the needed methods.
>> more…

Finally I found a case, where I really like C# regions: to structure unit tests. Usually there is more than one test per method and finding the place, where the testing of one method starts and the other ends is quite annoying.

In all other classes I still think that the only use of regions is to find which parts of a class should be extracted into a new class :)

Lately, we’ve been playing around with stylesheets for print and directly ran into some problems.

This snippet in Firefox

  1. <div style="page-break-after: always">
  2.    <!– first page –>
  3. </div>
  4. <div style="page-break-after: always">
  5.    <!– second page –>
  6. </div>

only prints exactly one page (that means it even stops in the middle of a line when the page is full).
>> more…

Last Saturday I visitied decoded, a conference dedicated to the beautiful things one can do with code. Topics ranged from art projects over open source hardware to more serious topics like visualization of data.

All talks were awesome (and I totally want an arduino for Christmas), but the one that kept me researching and having new ideas was the one of Moritz Stefaner about visualizing information. He has some really inspiring websites (here and here). I especially liked his contribution to the book “Dynamic Taxonomies and Faceted Search” – it’s about search, visualization and it even has formulas. What more could one want? >> more…

Did you ever want to use builders in your grails taglib and wondered why the approach mentioned in the grails documentation doesn’t work at all? Here is what I found out after a lot of trial and error and digging in the MarkupBuilder, BuilderSupport and GroovyPagTagBody sources. Let’s stick with the example from the documentation and correct it until it works (the impatient can scroll down):

  1. def dialog = { attrs, body ->
  2.   def markup = new groovy.xml.MarkupBuilder(out)
  3.   markup {
  4.     div(‘class’: ‘dialog’) {
  5.       body()
  6. } } }

>> more…

Grails claims that it encourages writing test – for example, a test class is created every time you create a controller or domain. However, working on my first Grails project I find it quite confusing which kind of test supports what.

For example, we use GreenMail in our testing environment. To check for sent mails, we have to use GrailsUnitTests, even if we are testing a Controller, because the content of mails is always empty in ControllerUnitTests.

Checking view names or redirects in those two types of tests is also slightly different:

  1. //ControllerUnitTest
  2. assert controller.modelAndView.view == "send"
  3. assert controller.redirectArgs.controller == "delivery"
  4. //GrailsUnitTests
  5. assert controller.modelAndView.view == "Message/send"
  6. assert controller.response.redirectUrl == "/delivery"

This is pretty annoying and I hope it will be unified in future versions of Grails. >> more…

Per default, Google Analytics tracks the whole URL. As a lot of our URLs contain some parameters or IDs, and we’re not interessted in tracking them, we needed to find a way to truncate them. As Grails unfortunately does not provide methods to access the current view and controller (which is basically what we’d like to track) in GSPs, we used this code:

  1. pageTracker._trackPageview(${request.getServletPath()‘/grails/’‘.dispatch’});

Not as clean as we’d like to have it, so suggestions are welcome!

Sometimes the easiest way is the right one – after a considerable time of research, trial and error we figured out, how to pretend to be a certain browser in a grails tests:

  1. controller.request.addHeader("USER-AGENT", "mozilla")

The information is stored in the request header, so the obvious solution to add an user-agent-header worked. That we didn’t even consider this solution at first is maybe a sign that testing in grails seldom does what you expect…

We are using Groovy on Grails for an incubator project, which is incredibly refreshing! Groovy as a dynamic language with optional type annotations doesn’t quite rock my boat though. With static type checking the types you define are an essential element of design. I like to point out that Groovy’s creator wrote:

“I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I’d probably have never created Groovy.” >> more…

Grails supports filters, which are applied to each action – before or after executing the action or after rendering. The scope of those filters can be constrained by a regex-like syntax to a subset of all actions.

That’s a pretty neat method to check for authentication, incorrect parameters etc. Unfortunately, filters are not applied during unit or integration tests – you need functional tests that simulate http-requests and responses to verify they are doing the right thing.

Grails does not support functional tests directly, you need a plugin (we chose “functional-test”): >> more…

Older Posts »