2014-11-04 175 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