4
votes

I have a controller I'm testing with Ember CLI, but the controller's promise will not resolve, as the controller's transitionToRoute method is returning 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"

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)
        )
    )

When the line @transitionToRoute("dashboard") is removed, the test passes; otherwise, the test fails.

How can I fix this error, while still maintaining my controller logic?

3
transitionToRoute is not returning null, it is null. this is not what you suspect it is, I imagine. My non-interest in coffeescript prevents me from worrying about it too much :) - Steve H.
If you found a solution, Please do post it as an answer, as am facing similar problem. - Mawaheb

3 Answers

2
votes

Work around: bypass transitionToRoute if target is null. Something like:

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

I ran into the same error and dug into Ember source code a little bit. In my case this error is thrown by ControllerMixin because get(this, 'target') is null at this line. The test module probably has no idea what target should be in a controller unit test like this without further context, so you may need to manually set it or just bypass it.

0
votes

Since you're not interested in the transition itself, you can just stub out the transitionToRoute method on the controller.

JS:

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

Coffee:

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

Not sure why transitionToRoute method is undefined when you execute it within an unit test - it is probably related to the fact that the execution context is different.

One possible workaround to this would be if you move your transitionToRoute call to the route instead of it being in the controller. That way your controller will send action to its route and you'll keep routing only in the route.

There is a big discussion around which is better practice - routing from controller or not but this is another story.