tags:

views:

44

answers:

3

I have two testing questions. Both are probably easily answered. The first is that I wrote this unit test in Grails:

void testCount() {
    mockDomain(UserAccount)

    new UserAccount(firstName: "Ken").save()
    new UserAccount(firstName: "Bob").save()
    new UserAccount(firstName: "Dave").save()

    assertEquals(3, UserAccount.count())
}

For some reason, I get 0 returned back. Did I forget to do something?

EDIT: OH, I understand. The validation constraints were violated, so they didn't store. Is there any way to get some feedback here? That's a really crappy thing to have happen....

The second question is for those who use IDEA. What should I be running - IDEA's junit tests, or grails targets? I have two options.

Also, why does IDEA say that my tests pass and it provides a green light even though the test above actually fails? This will really drive me nuts if I have to check the test reports in html every time I run my tests.....

Help?

+1  A: 

I personally haven't found the Idea JUnit tests to particularly useful when working with grails. It is likely fine to use the test runner for "Unit" tests. For integration tests you might consider setting up an ant target in "debug" mode to run your tests. Over time running tests starts to occupy such a long amount of time I tend to run them exclusively from the command line to avoid the additional overhead IntelliJ adds.

In regards to your unit test, I am pretty sure you would need to run an integration test to get a count that is not zero.

I'm not sure what unit test your using exactly but since GORM is not bootstrapped in the unit tests I'm not sure the domain object mocking supports the increment of a count.

Your test would likely pass as an integration test provided that your domain objects validate.

DanielHonig
I think the lack of running tests in my ide is really going to be a problem for me. I am a testing fanatic. I want really fast test cycles. I am used to working on projects where 1000+ tests only take 10-20 seconds, and I didn't even use mocks (it's all integration tests, because I'm not a fan of mocking).It seems that bootstrapping grails takes 10 seconds just by itself to run 1 test. It's rather disappointing. I am not a rails expert by any means... but rails has much faster turn around time here too, no? I'm just in the process of deciding what framework to use. Learning them all a bit.
egervari
+2  A: 

I always do object.save(failOnError: true) in tests to avoid silent failures like this. This causes an exception to be thrown if validation fails. Even without a real database in a unit test, most of the constraints will be checked, although I prefer to use integration tests if I want to test complex relationships between domain objects.

ataylor
Is there a way to get this behaviour by default without having to pass it in over and over when unit testing? Part of the problem with getting tests to pass is making sure the objects you are constructing just to test something else can be a massive pain and really ruin productivity. Man, this should have totally been the default. Makes me think, what were they thinking? :(
egervari
Add the line `grails.gorm.failOnError=true` to Config.groovy to make it the default.
ataylor
A: 

add flush:true to your save method.

new UserAccount(firstName: "Ken").save(flush:true)
...

Grails sets the flush mode of the hibernate session to manual. So the change is not persisted after the action returns but is before the view is rendered. This allows views to access lazy-loaded collections and relationships and prevents changes from automatically being persisted.

Lloyd Meinholz