测试目的:
- 测试路由是否可用,或者重定向
- 测试是否正确的 controller 处理路由
当测试路由时,我们希望确保的是我们定义的路由规则能够被正确的执行。因此我们需要确认路由是否正常工作,是否被自己拦截,是否重定向或者干脆直接找不到。当 $routeChangeError 事件被触发的时候,应该显示一个 404 页面。我们同样需要检查,是否模板被加载到了 View 中。最好的测试分类,应该是 Midway 测试 和 E2E 测试。
那么让我们来看看如何在 AngularJS 中,用 Midway 和 E2E 测试路由。
Midway 测试:
<!-- lang: js -->
//
// test/midway/routesSpec.js
//
describe("Midway: Testing Routes", function() {
var tester;
beforeEach(function() {
tester = ngMidwayTester('App');
});
afterEach(function() {
tester.destroy();
tester = null;
});
it("should have a working videos_path route", function() {
expect(ROUTER.routeDefined('videos_path')).to.equal(true);
var url = ROUTER.routePath('videos_path');
expect(url).to.equal('/videos');
});
it("should have a videos_path route that should goto the VideosCtrl controller", function() {
var route = ROUTER.getRoute('videos_path');
route.params.controller.should.equal('VideosCtrl');
});
it("should have a working video_path route", function() {
expect(ROUTER.routeDefined('video_path')).to.equal(true);
var url = ROUTER.routePath('video_path', { id : 10 });
expect(url).to.equal('/videos/10');
});
it("should have a videos_path route that should goto the VideoCtrl controller", function() {
var route = ROUTER.getRoute('video_path');
route.params.controller.should.equal('VideoCtrl');
});
it("should have a working watched_videos_path route", function() {
expect(ROUTER.routeDefined('watched_videos_path')).to.equal(true);
var url = ROUTER.routePath('watched_videos_path');
expect(url).to.equal('/watched-videos');
});
it("should have a videos_path route that should goto the WatchedVideosCtrl controller", function() {
var route = ROUTER.getRoute('watched_videos_path');
route.params.controller.should.equal('WatchedVideosCtrl');
});
it("should have a home_path route that should be the same as the videos_path route", function() {
expect(ROUTER.routeDefined('home_path')).to.equal(true);
var url1 = ROUTER.routePath('home_path');
var url2 = ROUTER.routePath('videos_path');
expect(url1).to.equal(url2);
});
it("should have a home_path route that should goto the VideosCtrl controller", function() {
var route = ROUTER.getRoute('home_path');
route.params.controller.should.equal('VideosCtrl');
});
});
E2E 测试
<!-- lang: js -->
//
// test/e2e/routesSpec.js
//
describe("E2E: Testing Routes", function() {
beforeEach(function() {
browser().navigateTo('/');
});
it('should jump to the /videos path when / is accessed', function() {
browser().navigateTo('#/');
expect(browser().location().path()).toBe("/videos");
});
it('should have a working /videos route', function() {
browser().navigateTo('#/videos');
expect(browser().location().path()).toBe("/videos");
});
it('should have a working /wathced-videos route', function() {
browser().navigateTo('#/watched-videos');
expect(browser().location().path()).toBe("/watched-videos");
});
it('should have a working /videos/ID route', function() {
browser().navigateTo('#/videos/10');
expect(browser().location().path()).toBe("/videos/10");
});
});