繁体   English   中英

Grails单元测试失败

[英]Grails Unit Test Failing

因此,我开始学习groovy和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)
    }

这是在“ test-app”时抛出的异常

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)

如果这个问题太幼稚,我感到抱歉。 请帮忙谢谢

在测试类中模拟域实例之前,它无法识别域类上的诸如save()之类的动态方法。

在测试类的类级别上使用@Mock(Product)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM