开发者

Help with failing Grails test - domain methods not working?

Given this service:

class AchievementsService {

 开发者_如何学编程   static transactional = true

    public void onEvent(String eventName, User user) {
        def event = Event.findByName(eventName)
        if (!event) {
            event = new Event(name: eventName, autoConfigured: true)
            event.save()
        }
    }
}

Why is this test failing:

class AchievementsServiceTests extends GrailsUnitTestCase {
    AchievementsService service
    User user
    protected void setUp() {
        super.setUp()
        service = new AchievementsService()
        user = new User(username:"marty",password: "password")
        mockDomain(User,[user])
    }

    void testThat_given_eventDoesNotExistWhenCallingOnEvent_that_eventIsCreated()
    {
         mockDomain Event       
         service.onEvent "MyEvent", user
         assert Event.count() == 1
    }
}

Fails as follows:

Assertion failed: 

assert Event.count() == 1
             |       |
             0       false

I must be missing a step either in my setup, or when calling my .save() method, but I can't see it.


You may have a validation failure, add this code to your service to print validation errors:

if (!event.save()) { 
  event.errors.each{println it} 
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜