测试 Requests / Pages

前后端请求测试实践
本文介绍了一种针对前后端请求的测试方法,包括Midway测试和E2E测试。通过具体的示例展示了如何确保页面请求正常运行、数据正确显示及路由和控制器功能验证。

测试目的

  • 测试每个页面的请求从开始到结束都正常运行
  • 测试每个页面加载的数据正常显示

测试页面/请求的目的和测试路由/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');
  });

});

转载于:https://my.oschina.net/ilivebox/blog/277501

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值