2013-08-27 62 views
0

我想在我的應用程序中測試路線設置。 我正在使用Qunit和燼測試助手。Ember RC7:測試路線

我有類似下面的測試:

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

的問題是,我無法在失敗和成功的訪問加以區分。 任何想法?

回答

0

我不是QUnit專家,但我想你可以採取一種方法是測試爲currentPath,而不是URL,可以從ApplicationController得到它,像這樣:

這個測試應該通過

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'); 
    }); 
}); 

但這應該失敗

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'); 
    }); 
}); 

簡單demo here

希望它有幫助。