unit-testing - Ember CLI Controller 测试 : Uncaught TypeError: Cannot read property 'transitionToRoute' of null

标签 unit-testing ember.js coffeescript qunit ember-cli

我有一个 Controller ,我正在使用 Ember CLI 进行测试,但 Controller 的 promise 无法解决,因为 Controller 的 transitionToRoute方法返回 null :

Uncaught TypeError: Cannot read property 'transitionToRoute' of null



login.coffee
success: (response) ->
    # ...

    attemptedTransition = @get("attemptedTransition")
    if attemptedTransition
        attemptedTransition.retry()
        @set "attemptedTransition", null
    else
        @transitionToRoute "dashboard"

登录-test.coffee
`import {test, moduleFor} from "ember-qunit"`

moduleFor "controller:login", "LoginController", {
}

# Replace this with your real tests.
test "it exists", ->
    controller = @subject()
    ok controller

###
    Test whether the authentication token is passed back in JSON response, with `token`
###
test "obtains authentication token", ->
    expect 2
    workingLogin = {
        username: "user@pass.com",
        password: "pass"
    }
    controller = @subject()
    Ember.run(->
        controller.setProperties({
            username: "user@pass.com",
            password: "pass"
        })
        controller.login().then(->
            token = controller.get("token")
            ok(controller.get("token") isnt null)
            equal(controller.get("token").length, 64)
        )
    )

当线@transitionToRoute("dashboard")被移除,测试通过;否则,测试失败。

如何修复此错误,同时仍保持我的 Controller 逻辑?

最佳答案

解决方法:绕过 transitionToRoute如果 targetnull .就像是:

if (this.get('target')) {
  this.transitionToRoute("dashboard");
}

我遇到了同样的错误并稍微研究了 Ember 源代码。在我的情况下,此错误是由 ControllerMixin 引发的。因为get(this, 'target')nullthis line .测试模块可能不知道 target 是什么应该在没有进一步上下文的情况下进行这样的 Controller 单元测试,因此您可能需要手动设置它或只是绕过它。

关于unit-testing - Ember CLI Controller 测试 : Uncaught TypeError: Cannot read property 'transitionToRoute' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587515/

相关文章:

unit-testing - 如何组织集成测试和单元测试

javascript - Ember JS : force transition to parent route

javascript - 如何通过 #each 循环使用 Ember.Object 实例

javascript - 重定向到 Ember 数组中的第一个对象

coffeescript - CoffeeScript 中的命名空间

java - 同时测试 Clojure 和 Java

c# - 我们如何使用第三方 API 为 Webapi 创建单元测试

Javascript "stuck"键(未注册的 keyup 事件)

javascript - CoffeeScript:使用 lodash(_ .js) 将 List 的 List 合并为单个 List

c# - 单元测试方法不好吗?