测试目的
- 测试每个页面的请求从开始到结束都正常运行
- 测试每个页面加载的数据正常显示
测试页面/请求的目的和测试路由/controller的目的是一样的。但这节的测试侧重点在于,你只需要简单的检查一下,路由是否访正确工作,controller是否正常执行,模板是否被下载下来,view 是否被正确渲染,并且标记为就绪。后台应该会触发有一大堆的请求和下载,因此,当你的 controller 标记你的页面(比如说在 body 标签上,加上一小段 html 标记,之类的...),你就知道了页面可用了。之后你就可以访问你的站点的所有页面和路由了。
测试主要用到 Midway 测试和 E2E 测试:
Midway 测试
<!-- lang: js -->
//
// test/midway/requestsSpec.js
//
describe("Midway: Testing Requests", function() {
var tester;
beforeEach(function() {
if(tester) {
tester.destroy();
}
tester = ngMidwayTester('App');
});
it("should goto the videos_path by default", function(done) {
tester.visit('/', function() {
expect(tester.viewElement().html()).to.contain('app-youtube-listings');
done();
});
});
it("should have a working video_path request", function(done) {
var url = ROUTER.routePath('video_path', { id : 10 });
tester.visit(url, function() {
var $params = tester.inject('$routeParams');
expect(parseInt($params.id)).to.equal(10);
expect(tester.viewElement().html()).to.contain('app-youtube-profile');
done();
});
});
it("should have a working other_path request", function(done) {
var url = ROUTER.routePath('other_path');
tester.visit(url, function() {
expect(tester.viewElement().html()).to.contain('other page');
done();
});
});
});
E2E 测试:
<!-- lang: js -->
//
// test/e2e/requestsSpec.js
//
describe("E2E: Testing Requests", function() {
beforeEach(function() {
browser().navigateTo('/');
});
it('should have a working /videos page', function() {
browser().navigateTo('#/');
expect(browser().location().path()).toBe("/videos");
expect(element('#ng-view').html()).toContain('data-app-youtube-listings');
});
it('should have a working /other page', function() {
browser().navigateTo('#/other');
expect(browser().location().path()).toBe("/other");
//try removing the controller and this will fail
expect(element('#ng-view').html()).toContain('success');
});
});