【发布时间】:2016-12-16 13:01:46
【问题描述】:
我使用 Protractor 是为了在我的应用程序中进行一些端到端测试。在测试中,我使用 MockModule 模拟后端调用,如下所示:
describe('Lets test a feature' , function(){
beforeAll(function () {
var customerE2E = function () {
angular.module('customerE2E', ['customer', 'ngMockE2E'])
.run(function ($httpBackend) {
$httpBackend.whenPOST('/api/data').respond(200);
$httpBackend.whenGET(/^.*/).passThrough();
});
};
browser.addMockModule('customerE2E', customerE2E);
});
it('correctly demonstrates my problem', function () {
expect(element(by.css('h4')).getText()).toMatch('Hello world');
}
})
这确实很好用,但我的问题是,当帖子以 404、500 等响应时,我还想测试我的应用程序。我已经通过为每种情况设置一个新的“描述”功能来解决这个问题,但它会是很高兴能够从“it”函数内部覆盖这个调用。
it('correctly demonstrates my problem', function () {
expect(element(by.css('h4')).getText()).toMatch('Hello world');
}
it('show error due to bad request', function () {
/*
Replace $httpBackend.whenPOST('/api/data').respond(200);
with $httpBackend.whenPOST('/api/data').respond(404);
*/
expect(element(by.css('h4')).getText()).toMatch('Failed api call');
}
我对此真的很陌生,所以我想知道是否有一种很好的方法来实现一种简单的方法来覆盖在 beforeAll 函数中设置的早期 MockModuled。
【问题讨论】:
标签: angularjs testing jasmine protractor integration-testing