2012-01-03 167 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爲空,我不能調用size方法就可以了。我在這裏做錯了什麼?

+1

一些指針 - 你不應該需要創建控制器的一個實例。假設你的測試類叫做AddressControllerTests,AddressController的一個實例應該已經可以通過變量'controller'獲得了。另外,我不會測試模型的大小,我會測試響應; controller.response ???。測試控制器,你想測試你回來的迴應。 – Gregg 2012-01-03 16:23:38

+0

這些都是正確的指針。我需要測試列表中的實際內容,但首先我只是測試列表是否具有正確的大小,這應該更容易測試即使這不起作用。 – 2012-01-03 16:53:09

回答

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()適用於參數類型:()values:[ ]可能的解決方案:wait(),any(),wait(long),use([Ljava.lang.Object;),isCase(java.lang.Object),each(groovy.lang.Closure) – 2012-01-03 16:55:20

+0

我試過類似這個: 地址a1 = new thlc.Addresses(firstLine:'A1',secondLine:'B',thirdLine:'C',luCountry:UnitedStates,zipCode:'12345',luState:Florida,city:'jag') ; 地址a2 = new thlc.Addresses(firstLine:'A2',secondLine:'B2',thirdLine:'C2',luCountry:UnitedStates,zipCode:'12344',luState:Florida,city:'jag2'); \t mockDomain(Addresses,[a1,a2]); a1.save(); a2.save(); 但即使這不起作用,它給出了和以前一樣的問題。 – 2012-01-03 16:58:45

+1

你正在給你的測試添加邏輯,在我看來這是一個壞主意。你提出的解決方案的問題是對象可能被添加到模擬數據庫中,它們可能不是。如果您的產品代碼不正確,或者由於某種原因您的對象未通過驗證,您的測試將失敗。對於這個單元測試,我們不是測試對象的有效性,而是控制器方法。如果你想確保有效的對象,你可以使用.save(failOnError:true),它將在保存點而不是在斷言處失敗。我的2美分。 – 2012-01-03 19:27:25

2

你錯誤地訪問模型。在一個單元測試,你應該訪問通過模型:

def model = controller.modelAndView.model 

然後訪問任何你想要關閉模式,使您的情況這將是:

ac.list() 
def model = ac.modelAndView.model 
assertEquals(2, model.postInstanceList.size())