2012-01-03 4 views
0

컨트롤러의 목록 동작에 대한 단위 테스트를 시도하고 있습니다. 여기 테스트를위한 코드는 다음과 같습니다단위 테스트 컨트롤러 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 것과 같은 결과를 가져 오는, 그리고 난 그것에 크기 메소드를 호출 할 수 없습니다 시도하는 방법에 상관없이. 여기서 내가 뭘 잘못하고 있니?

+1

일부 포인터 - 당신이 필요가 없습니다 컨트롤러의 인스턴스를 만듭니다. 테스트 클래스가 AddressControllerTests라고 가정하면, AddressController 인스턴스는 이미 'controller'변수를 통해 사용 가능해야합니다. 또한 모델의 크기를 테스트하지 않고 응답을 테스트합니다. controller.response. ???. 컨트롤러를 테스트하면 돌아 오는 응답을 테스트하려고합니다. – Gregg

+0

이들은 모두 올바른 포인터입니다. 목록의 실제 내용을 테스트해야하지만, 목록의 크기가 올바른지 아닌지는 테스트 중이었습니다. 테스트가 쉬워야합니다. 심지어 작동하지 않습니다. –

답변

1

인스턴스를 저장하지 않습니다. 당신은 저장한다 :

mockDomain(Addresses) 
new Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag').save() 

new Addresses(firstLine:'A2', secondLine:'B2', thirdLine:'C2', luCountry:UnitedStates, zipCode:'12344', luState:Florida, city:'jag2').save() 

내가 이런 식으로 할 것 :

mockDomain(Addresses) 
mockForContraintsTests(Addresses) 
def address1 = new Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag') 
if(address1.validate()) address1.save() 
def address2 = new Addresses(firstLine:'A2', secondLine:'B2', thirdLine:'C2', luCountry:UnitedStates, zipCode:'12344', luState:Florida, city:'jag2') 
if(address2.validate()) address2.save() 


assertEquals 2, Addresses.list().size() 
+0

그게 작동하지 않을 때, 이런 식으로 넣으려고 할 때, 오류가 발생했습니다 : groovy.lang.MissingMethodException : 메소드의 서명이 없습니다 : thlc.Addresses.save()는 인수 유형에 적용 가능합니다 :() 값 : [ ] 가능한 해결책 : wait(), any(), wait (long), use ([Ljava.lang.Object]), isCase (java.lang.Object), each (groovy.lang.Closure) –

+0

이 : 주소 a1 = new thlc.Addresses (firstLine : 'A1', secondLine : 'B', 세 번째 줄 : 'C', 루프트 한자 : 미국, zipCode : '12345', luState : 플로리다, 도시 : 'jag') ; 주소 a2 = 새로운 thlc.Addresses (firstLine : 'A2', secondLine : 'B2', thirdLine : 'C2', luCountry : UnitedStates, zipCode : '12344', luState : 플로리다, city : 'jag2'); \t mockDomain (주소, [a1, a2]); a1.save(); a2.save(); 그러나 이것도 작동하지 않습니다. 이전과 같은 문제를 일으 킵니다. –

+1

당신은 내 의견으로는 나쁜 생각 인 당신의 테스트에 논리를 추가하고 있습니다. 제안 된 솔루션의 문제점은 객체가 모의 데이터베이스에 추가 될 수 있고 그렇지 않을 수도 있다는 것입니다. 프로덕션 코드가 잘못되었거나 어떤 이유로 인해 개체가 유효성 검사를 통과하지 못하면 테스트가 어설 션에서 실패합니다. 이 단위 테스트에서는 객체의 유효성을 테스트하는 것이 아니라 컨트롤러 메소드를 테스트합니다. 유효한 객체를 보장하려면 .save (failOnError : true)를 사용하면 저장 시점에서 실패하고 assert 상태에서는 실패합니다. 내 2 센트. –

2

당신은 잘못 모델에 접근하고 있습니다. 그것이 될 것입니다 인스턴스에 있도록 모델의 떨어져 원하는

def model = controller.modelAndView.model 
다음

액세스 : 단위 테스트에서 당신은을 통해 모델에 액세스해야

ac.list() 
def model = ac.modelAndView.model 
assertEquals(2, model.postInstanceList.size()) 
관련 문제