Ember CLI Controller Test: Uncaught TypeError: Unable to read the 'transitionToRoute' property from null

I have a controller that I am testing with the Ember CLI, but the promise of the controller will not be resolved since it returns the controller transitionToRoute null:

Uncaught TypeError: Cannot read the 'transitionToRoute' property from 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: "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)
        )
    )

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

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

+4
source share
3 answers

: transitionToRoute, target - null. - :

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

Ember. ControllerMixin, get(this, 'target') null . , , target unit test, , , .

+2

, 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 undefined, unit test - , , .

One possible workaround for this would be if you move your ToRoute transient call to the route instead of being in the controller. Thus, your controller will send the action to its route, and you will continue routing only on the route.

There is a lot of discussion around which it is better to practice whether routing from the controller or not, but this is another story.

0
source

All Articles