2014-08-30 51 views
4

我有一个控制器我与Ember CLI测试的特性“transitionToRoute”,但控制器的承诺不会解决,因为控制器的transitionToRoute方法返回null灰烬CLI控制器测试:遗漏的类型错误:无法读取空

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" 

login-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: "[email protected]", 
     password: "pass" 
    } 
    controller = @subject() 
    Ember.run(-> 
     controller.setProperties({ 
      username: "[email protected]", 
      password: "pass" 
     }) 
     controller.login().then(-> 
      token = controller.get("token") 
      ok(controller.get("token") isnt null) 
      equal(controller.get("token").length, 64) 
     ) 
    ) 

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

如何解决此错误,同时仍然保持我的控制器逻辑?

+0

'transitionToRoute'不返回null,它*为* null。我猜想这不是你所怀疑的。我对coffeescript不感兴趣,无法让我担心它:) – 2014-08-31 02:47:20

+0

如果您找到了解决方案,请将其作为答案发布,因为我面临类似的问题。 – Mawaheb 2014-12-18 14:23:04

回答

2

变通方法:绕过transitionToRoute如果targetnull。例如:

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

我遇到了相同的错误,并且稍微挖了一点Ember源代码。在我的情况下,这个错误是由ControllerMixin抛出,因为get(this, 'target')nullthis line。测试模块可能不知道什么target应该在这样的控制器单元测试中没有进一步的上下文,因此您可能需要手动设置它或绕过它。

0

由于您对转换本身并不感兴趣,因此您可以将transitionToRoute方法存放在控制器上。

JS:

test('Name', function() { 
    var controller = this.subject(); 
    controller.transitionToRoute = Ember.K; 
    ... 
} 

咖啡:

test "it exists", -> 
    controller = @subject() 
    controller.transitionToRoute = Ember.K 
    ok controller 
0

不知道为什么,当你在单元测试中执行它transitionToRoute方法是不确定的 - 它可能涉及到的事实,执行上下文不同。

对此的一个可能的解决方法是如果您将transitionToRoute调用移动到路由而不是它在控制器中。这样你的控制器就会把动作发送到它的路由,并且你只会在路由中保持路由。

围绕哪个更好的实践有一个大讨论 - 从控制器路由或不是,但这是另一回事。

相关问题