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}
}
精彩评论