unit-testing - 单元测试 Grails Controller 链接

标签 unit-testing grails controller chain

我无法弄清楚如何测试执行“链接”的 Controller 操作。我想验证这个 Action 。

chalice :2.4.2

Controller :

class MyController {

def index() {

}

def doesChain() {
    chain action: 'index', model: [name: "my name"]
}

测试:

@TestFor(MyController)
class MyControllerSpec extends Specification {

def setup() {
}

def cleanup() {
}

void "Action doing chain"() {

    when:
    controller.doesChain()

    then:
    controller.chainModel.name == "my name"
    controller.actionName == "someAction" // fails as actionName == null
}

测试操作名称未通过,因为 actionName 似乎为空。

最佳答案

你可以做这样的事情......

@TestFor(MyController)
class MyControllerSpec extends Specification {

    void "Action doing chain"() {

        when: 'an action invokes the chain method'
        controller.doesChain()

        then: 'the model is as expected'

        // either of these should work, you don't need both...
        controller.chainModel.name == "my name"
        flash.chainModel.name == "my name"

        and: 'the redirect url is as expected'
        response.redirectUrl == '/my/index'

    }
}

希望对您有所帮助。

关于unit-testing - 单元测试 Grails Controller 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24909675/

相关文章:

javascript - Sinon stub 定义在同一文件中的函数

php - 使用 MongoDB 和对象数组进行 Laravel 单元测试

java - Eclipse 中的 JUnit 测试

grails - 尝试从 Grails 应用程序中的长数字中删除逗号

grails Controller 渲染错误

session - 使用 session 测试 Rails Controller

reactjs - 模拟意味着在 1 个节点上运行 = 0 找到

grails - 如何使用 groovy 调用 Grails 中的服务

json - 使用对象的部分属性在 GRAILS 中呈现 JSON

php - 无法从 URL 中获取变量