unit-testing - 模拟时,Grails单元测试通过引用不起作用

标签 unit-testing grails mocking spock

我在模拟服务类以保存域的单元测试中。最初,我的 Controller 方法如下所示:

def save(Long organizationId, Convention convention) {
  conventionService.save(organizationId, convention)

  if (convention.hasErrors()) {
    response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
    respond convention.errors
  } else {
    response.status = HttpStatus.CREATED.value()
    respond convention
  }
}

通常,这是可行的,因为Java是通过引用传递的,所以传递给save方法的convention在整个方法中都是相同的convention对象。但是,在模拟conventionService.save方法时,按引用传递不起作用。调整我的方法以解决此问题:
def save(Long organizationId, Convention convention) {
  convention = conventionService.save(organizationId, convention)

  if (convention.hasErrors()) {
    response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
    respond convention.errors
  } else {
    response.status = HttpStatus.CREATED.value()
    respond convention
  }
}

允许我的测试通过,因为convention对象正是我的模拟所期望的:
1 * service.save(1, _) >> new Convention(
       id: 1,
       name: 'Con 1',
       description: 'This is a pretty cool convention, everyone should go',
       startDate: new Date(),
       endDate: new Date()+10,
       organization: organization)

我的问题是,这是预期的行为还是我应该报告的错误?

最佳答案

My question is, is this expected behavior or a bug that I should be reporting?



这是预期的行为。这不是您应该报告的错误。

关于unit-testing - 模拟时,Grails单元测试通过引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454347/

相关文章:

java - 如何使assertEquals方法中的预期对象成为一个姿势(在函数中,请参阅下面的代码)而不是整数或 double ?

使用接口(interface)定义spring bean

grails - Grails gsp文件太大。 “Method code too large!”运行时错误

python - 如何将 mock.patch 移动到 setUp?

php - 如何模拟一个变量通过引用传递给 PHPUnit 的函数?

c# - MVC Web 解决方案上的 Log4Net

unit-testing - Apache Camel bean 单元测试

unit-testing - 单元测试具有许多私有(private)方法的复杂类

javascript - Jest 监视模块功能

Grails Spring-Security-Core 插件 - 无法验证用户