我有一个 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
如果 target
是 null
.就像是:
if (this.get('target')) {
this.transitionToRoute("dashboard");
}
我遇到了同样的错误并稍微研究了 Ember 源代码。在我的情况下,此错误是由
ControllerMixin
引发的。因为get(this, 'target')
是 null
在 this line .测试模块可能不知道 target
是什么应该在没有进一步上下文的情况下进行这样的 Controller 单元测试,因此您可能需要手动设置它或只是绕过它。
关于unit-testing - Ember CLI Controller 测试 : Uncaught TypeError: Cannot read property 'transitionToRoute' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587515/