Tuesday, 22 May 2012

A thought on testing Grails controllers

I'm writing tests for my grails app, and I found a handy shortcut when you are testing your controllers and don't want to populate all of the domain object under test's fields. You can simply set the validate field to be false on a domain object:



1:       void testEditExistingBusiness() {  
2:            def user = new User(username : 'tommy')  
3:            user.save(validate: false)  
4:            def business = new Business(id : 1, user : user)  
5:            business.save(validate: false)  
6:            def securityMock = createSpringSecurityMock(user)  
7:            controller.springSecurityService = securityMock  
8:            params.id = business.id  
9:            def model = controller.edit()  
10:            assert model.businessInstance == business  
11:       }  


That way you don't have to populate all of the User's and Business's required fields, which in this case is perfectly fine for my test.

No comments:

Post a Comment