为什么ngmocke2e的后续测试不能调用模拟后台?只有第一个测试才能通过。第二个测试将调用真正的后端。
下面是我的示例代码:第一个测试将调用mock。第二个将调用真正的后端。
var LoginPage = require( './login_page' );
describe('As a user, when using valid credentials', function() {
var login = new LoginPage();
beforeEach(
function () {
browser.addMockModule('myService', function() {
angular
.module('myService', ['myApp', 'ngMockE2E'])
.run(function($httpBackend) {
var access={"access_token":"a", "token_type":"bearer", "expires_in":299, "refresh_token":"a", "userName":"any", "clientId":"blah", ".issued":"Mon, 08 Jun 2015 20:47:40 GMT", ".expires":"Mon, 08 Jun 2015 20:52:40 GMT"};
$httpBackend.whenPOST("https://blah.com/OAuth2Server/1.0/OAuth/Token").respond(200, access);
$httpBackend.whenGET(/\/*/).passThrough();
$httpBackend.whenPOST().passThrough();
});
});
});
it('logins successfully', function() {
login
.navigate()
.login("anything", "password");
browser.sleep(5000);
browser.ignoreSynchronization=true;
var currentUrl=browser.getCurrentUrl();
expect(currentUrl).toBe("http://localhost:55555/#/my-jobs");
});
});
describe('As a user, when using valid credentials', function() {
var login = new LoginPage();
beforeEach(
function () {
browser.addMockModule('myService', function() {
angular
.module('myService', ['myApp', 'ngMockE2E'])
.run(function($httpBackend) {
var access={"access_token":"a", "token_type":"bearer", "expires_in":299, "refresh_token":"a", "userName":"any", "clientId":"blah", ".issued":"Mon, 08 Jun 2015 20:47:40 GMT", ".expires":"Mon, 08 Jun 2015 20:52:40 GMT"};
$httpBackend.whenPOST("https://blah.com/OAuth2Server/1.0/OAuth/Token").respond(200, access);
$httpBackend.whenGET(/\/*/).passThrough();
$httpBackend.whenPOST().passThrough();
});
});
});
it('logins successfully', function() {
login
.navigate()
.login("anything2", "password2");
browser.sleep(5000);
browser.ignoreSynchronization=true;
var currentUrl=browser.getCurrentUrl();
expect(currentUrl).toBe("http://localhost:55555/#/my-jobs");
});
});发布于 2017-02-09 16:53:07
我不知道为什么会发生这个问题(也发生在我身上),但我能够通过在每个描述的末尾重新启动浏览器来“修复”它。只是重新加载它对我来说不起作用。
在每个describe中添加以下内容:
afterAll(function() {
// need this to avoid problems with ngmocke2e
browser.restart();
});我知道这不是一个理想的解决方案,而且它增加了一些额外的测试时间,但这就暂时解决了问题。
https://stackoverflow.com/questions/31065846
复制相似问题