开发者

Grails Testing hickups

开发者 https://www.devze.com 2022-12-31 21:51 出处:网络
I have two testing questions. Both are probably easily answered. The 开发者_高级运维first is that I wrote this unit test in Grails:

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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消