컨트롤러의 목록 동작에 대한 단위 테스트를 시도하고 있습니다. 여기 테스트를위한 코드는 다음과 같습니다단위 테스트 컨트롤러 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가 null 것과 같은 결과를 가져 오는, 그리고 난 그것에 크기 메소드를 호출 할 수 없습니다 시도하는 방법에 상관없이. 여기서 내가 뭘 잘못하고 있니?
일부 포인터 - 당신이 필요가 없습니다 컨트롤러의 인스턴스를 만듭니다. 테스트 클래스가 AddressControllerTests라고 가정하면, AddressController 인스턴스는 이미 'controller'변수를 통해 사용 가능해야합니다. 또한 모델의 크기를 테스트하지 않고 응답을 테스트합니다. controller.response. ???. 컨트롤러를 테스트하면 돌아 오는 응답을 테스트하려고합니다. – Gregg
이들은 모두 올바른 포인터입니다. 목록의 실제 내용을 테스트해야하지만, 목록의 크기가 올바른지 아닌지는 테스트 중이었습니다. 테스트가 쉬워야합니다. 심지어 작동하지 않습니다. –