1
鑑於這種服務:幫助失敗的Grails測試 - 域方法無效?
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()
}
}
}
爲什麼這個測試失敗:
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
}
}
失敗如下:
Assertion failed:
assert Event.count() == 1
| |
0 false
我必須缺少一個步驟,無論是在我的設置,或當打電話給我的.save()
方法,但我看不到它。
你得到事件的任何驗證錯誤?添加這個到你的服務來測試:if(!event.save()){event.errors.each {println it}}' – 2011-05-06 16:26:59
如果你把它作爲一個集成測試來運行它會工作嗎?在轉換爲集成測試後,您可能需要刪除對「mockDomain」的調用 – 2011-05-06 16:31:25
@David - 謝謝,我在模型中缺少一個約束。將其轉換爲答案,我會接受它。 – 2011-05-07 14:59:58