【问题标题】:Grails 2.1 Unit Testing Command Object mockForConstraintsTests not working?Grails 2.1 单元测试命令对象 mockForConstraintsTests 不起作用?
【发布时间】:2023-03-22 01:27:01
【问题描述】:

我已经为此命令对象使用了手动编写的以及 Grails 生成的单元测试:

   package myapp

    @grails.validation.Validateable
    class SearchCommand {
       String basisBuild
       String buildToSearch

       static constraints = {
          basisBuild(blank: false)
       }
    }

在我的手写单元测试失败后,我使用了 Grails:

create-unit-test  myapp.SearchCommand

我填写了单元测试,并根据关于模拟约束的文档做出了一个应该通过的断言:

package myapp
import static org.junit.Assert.*

import grails.test.mixin.*
import grails.test.mixin.support.*
import org.junit.*

@TestMixin(GrailsUnitTestMixin)
class SearchCommandTests {

    void setUp() {
        mockForConstraintsTests(SearchCommand)
    }

    void tearDown() {
        // Tear down logic here
    }

    void testSomething() {
        SearchCommand commandUnderTest = new SearchCommand()

        commandUnderTest.validate(basisBuild: "")

        assertEquals "blank", commandUnderTest.errors['basisBuild']
    }
}

为什么我会失败?

grails> test-app
| Running 9 unit tests... 9 of 9
| Failure:  testSomething(com.siemens.soarian.sf.gap.SearchCommandTests)
|  java.lang.AssertionError: expected:<blank> but was:<null>
        at org.junit.Assert.fail(Assert.java:93)

【问题讨论】:

    标签: unit-testing grails mocking constraints


    【解决方案1】:

    我相信我在 grails 2.0 中找到了 grails 支持的对 Command 对象进行单元测试的方法。您需要使用 ControllerUnitTestMixin 提供的 mockCommandObject。

    感谢埃里克

    http://www.jworks.nl/2012/04/12/testing-command-objects-in-grails-2-0/

    【讨论】:

      【解决方案2】:

      编辑

      如果现有Grails bug 中提到的补丁已经到位,则适当地使用validate()mockForConstraintsTest 应该可以工作(感谢@codelark 提出)。为了从 Web 应用的角度测试命令对象(使用 controller),以下信息会有所帮助。

      使用控制器操作测试命令对象:-

      command 对象仅在用作controller 内的action 方法之一的参数时才被视为此类对象。请参阅Command Objects(警告说明)。 在action方法中使用SearchCommand,应该可以assertEquals

      示例:

      void testSomething() {
              YourController controller = mockController(YourController) //Or instantiate
              SearchCommand commandUnderTest = new SearchCommand ()
              //Note the usage here. validate() does not take parameters
              commandUnderTest.basisBuild = ''
              commandUnderTest.validate()
      
              //Call your action
              controller.searchCommandAction(commandUnderTest)
      
              assert response.text == 'Returned'
              assertEquals "blank", commandUnderTest.errors['basisBuild']
          }
      

      YourController 的操作:-

      def searchCommandAction(SearchCommand sc){
          render "Returned"
      }
      

      注意:

      没有来自 grails 错误的补丁,我们在@Grails 2.1.4、2.2.0 和 2.2.1 中看到以下错误

      当我只更正validation 并使用mockForConstraintTests 而不使用controller action 时出现错误:

      【讨论】:

      • 我希望我的编辑会有所帮助。我很想知道它如何表现不同。 :)
      【解决方案3】:

      您错误地使用了validate 方法。您从未在类上设置字段,因此该字段为空,而不是空白。尝试如下更改您的测试:

      void testSomething() {
          SearchCommand commandUnderTest = new SearchCommand()
          commandUnderTest.basisBuild = ""
      
          assertFalse commandUnderTest.validate()
          assertEquals 'blank', commandUnderTest.errors['basisBuild']
      }
      

      编辑:在测试使用@Validatable 注释的命令类时,还有一个grails bug。错误评论中有一些解决方法。

      【讨论】:

      • 我已经在我的回答中提到了validate() 方法更正。此外,在 Grails 2.x 中,mockForConstraintsTests 必须替换为mockDomainObject(来自ControllerUnitTestMixins)或实例化(如我的回答所示)。按照您提出的修复程序,assertFalse commandUnderTest.validate() 会出现 assertion 失败。
      • 这是从 grails 2.2.1 中的一个工作示例中提取的。为什么说mockForConstraintsTest必须在2.x中替换?它仍在非弃用的测试 API See Javadoc
      • 我试图证明自己跟随你是错误的,但它在 2.1.4、2.2.0 和 2.2.1 中从未对我有用。创建了一个全新的项目,创建了命令对象,创建了测试,运行了测试。开导我。 :)
      • pastebin.com/zZRKhC06 - 包含您需要在 grails create-app 之后创建的两个文件。然后grails test-app :unit
      • @dmahapatro 看来我们结果的差异是由于一个未解决的 grails 错误。 See here. 显然注解和模拟助手之间存在交互问题。请注意,我在 pastebin 中的代码 not 使用了注释。我会用更多的细节更新我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2018-07-03
      相关资源
      最近更新 更多