为什么 ngmocke2e 的后续测试无法调用模拟后端?只有第一个测试会通过。第二个测试将调用真正的后端。
这是我的示例代码:第一个测试将调用模拟。第二个将调用真正的后端。
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");
});
});