小编fer*_*gjo的帖子

Grails 2.1单元测试命令对象mockForConstraintsTests不工作?

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

   package myapp

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

       static constraints = {
          basisBuild(blank: false)
       }
    }
Run Code Online (Sandbox Code Playgroud)

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

create-unit-test  myapp.SearchCommand
Run Code Online (Sandbox Code Playgroud)

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

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']
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我会失败?

grails> test-app
| Running 9 unit tests... 9 of 9 …
Run Code Online (Sandbox Code Playgroud)

grails unit-testing constraints mocking

3
推荐指数
1
解决办法
3964
查看次数

Grails 2.3控制器方法返回JSON在单元测试中返回null

让我首先说Grails 2.2上存在同样的问题.我在Windows 7上运行Grails 2.2,在家里我通过Homebrew安装在OSX 10.8.4上运行Grails 2.3.两种情况都会出现同样的问题.我的控制器看起来像这样:

package play

import grails.converters.JSON

class HelloJsonController {

    def greet() { 
        def greeting = new Greeting(greeting: 'Hey there')
        render greeting as JSON
    }
}
Run Code Online (Sandbox Code Playgroud)

我的POGO(上面使用的)就是这样:

package play

class Greeting {
    String greeting
}
Run Code Online (Sandbox Code Playgroud)

单元测试 - 我知道应该失败但是由于错误的原因而失败的是:

package play

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(HelloJsonController)
class HelloJsonControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test that the controller can greet in JSON"() {
        when: 'you call the greet action'
        def resp = …
Run Code Online (Sandbox Code Playgroud)

grails json unit-testing

2
推荐指数
1
解决办法
2593
查看次数

标签 统计

grails ×2

unit-testing ×2

constraints ×1

json ×1

mocking ×1