2014-11-04 239 views
0

因此,我開始學習groovy & grails。我想要編寫單元測試我的控制器是這樣的:Grails單元測試失敗

void testSave() { 

    params.productName = 'ProdName' 
    params.productBarCode = '123' 
    params.productStore = 'ProdStore' 
    def response = controller.save() 
    assert response.productInstance.productName == 'ProdName' 

} 

,這是控制器動作

def save() { 
     def productInstance = new Product(params) 
     if (!productInstance.save(flush: true)) { 
      render(view: "create", model: [productInstance: productInstance]) 
      return 
     } 

     flash.message = message(code: 'default.created.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id]) 
     redirect(action: "show", id: productInstance.id) 
    } 

,這是它拋出時,「測試應用程序」

groovy.lang.MissingMethodException: No signature of method: xxx.Product.save() is applicable for argument types:() values: [] 
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long) 
    at xxx.ProductController.save(ProductController.groovy:59) 
    at xxx.ProductControllerTests.testSave(ProductControllerTests.groovy:35) 
異常

對不起,如果這個問題太天真了。請幫助 謝謝

回答

2

直到域實例在測試類中被嘲笑,它將無法識別域類上的save()等動態方法。

在測試類中的類級別使用@Mock(Product)

+0

非常感謝... – dosdebug 2014-11-04 13:06:01