0
votes

I would like to test the route setup in my app. I'm using Qunit and the ember-testing helpers.

I have a test like the following:

test("visit can handle wrong urls", function() {
    var urlToVisit = "/foo/bogus";
    visit(urlToVisit).then(function () {
        // this will show me /foo/bogus even though the route does not exist
        console.log(app.__container__.lookup('router:main').location.path);
    });
});       

The problem is, I can't distinguish between a failed and a successful visit. Any ideas?

1

1 Answers

0
votes

I'm not a QUnit expert but I guess one approach you could take is to test for the currentPath instead of the URL, you can get it from the ApplicationController, something like this:

This test should pass

test("visit 'index' and assert it exists", function() {
  var urlToVisit = "/";
  visit(urlToVisit).then(function () {
    equal(App.__container__.lookup('controller:application').get('currentPath'), 'index', 'At route index');
  });
});

But this should fail

test("visit wrong URL and fail", function() {
  var urlToVisit = "/posts";
  visit(urlToVisit).then(function () {
    equal(App.__container__.lookup('controller:application').get('currentPath'), 'posts', 'Fail at non existent route posts');
  });
});

Simple demo here.

Hope it helps.