我正在尝试对控制器的列表动作进行单元测试。下面是测试它的代码: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