我正在嘗試對控制器的列表動作進行單元測試。下面是測試它的代碼:Grails中的單元測試控制器
void testListAction()
{
ac = new AddressesController();
def org = new Organizations(viewAllPost: true);
mockForConstraintsTests(Addresses);
def a = new Addresses(firstLine:'A', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag');
assertTrue(a.validate());
mockSession['currentUserOrganizationId'] = org;
mockDomain(Addresses, [
new Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag'),
new Addresses(firstLine:'A2', secondLine:'B2', thirdLine:'C2', luCountry:UnitedStates, zipCode:'12344', luState:Florida, city:'jag2')
]);
def model = ac.list();
assertEquals(2, model.postInstanceList.size());
}
但無論我怎樣努力,我始終取回相同的結果,該model.postInstanceList爲空,我不能調用size方法就可以了。我在這裏做錯了什麼?
一些指針 - 你不應該需要創建控制器的一個實例。假設你的測試類叫做AddressControllerTests,AddressController的一個實例應該已經可以通過變量'controller'獲得了。另外,我不會測試模型的大小,我會測試響應; controller.response ???。測試控制器,你想測試你回來的迴應。 – Gregg 2012-01-03 16:23:38
這些都是正確的指針。我需要測試列表中的實際內容,但首先我只是測試列表是否具有正確的大小,這應該更容易測試即使這不起作用。 – 2012-01-03 16:53:09