unit-testing - beforeInterceptor 的测试用例

标签 unit-testing grails testing integration-testing spock

以下是我的beforeInterceptor

def beforeInterceptor = {
    log.debug "============${actionName}=========="       
    if (!(actionName in ['search'])) {
        redirect controller: 'error', action: 'denied'
        return false
    }
}

我不知道如何编写 beforeInterceptor 的测试。我用谷歌搜索但没有运气。我已经尝试自己编写它的测试用例,但无法在 beforeInterceptor 中获取 actionName

when:
    controller.beforeInterceptor()

when:
    controller.beforeInterceptor.call()
    controller.history(ticket)

每次我在 actionName 中得到 null。

有人知道:如何编写 beforeInterceptor 的测试用例(integrationunit)?

最佳答案

我为此创建了单元测试,我们可以在那里的请求中设置操作名称。我直接调用了 beforeInterceptor:

@TestFor(PersonController)
@Mock([Person])
class PersonControllerSpec extends Specification {

  def setup() {
  }

  def cleanup() {
  }

  void "test beforeInterceptor"() {
    when:
    webRequest.actionName = "index"
    controller.beforeInterceptor()

    then:
    controller.response.redirectedUrl == '/error/denied'

    when:
    controller.response.reset()
    webRequest.actionName = "search"
    controller.beforeInterceptor()

    then:
    controller.response.redirectedUrl != '/error/denied'
  }
}

关于unit-testing - beforeInterceptor 的测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24012938/

相关文章:

java - 部署在tomcat上的grails war没有连接到数据库

mongodb - 将域类从 GORM 独立模块导入 Grails

java - 无法识别 TestFX clickOn 方法

php - 使用 PHPUnit 断言异常

ruby-on-rails - 开始对 Ruby Rails 网站进行单元和功能测试的最佳方法是什么?

node.js - node.js 的断言库?

xml - 如何使用Grails用XML文件填充数据库

ruby-on-rails - 如何在 Rails 3 中进行集成测试?

python - 如何删除 Python 类中隐式传递的 self ?

javascript - 无法使用 Backbone.history.start() 和 QUnit 调用未定义错误的方法 'start'