I'm adding a mockmodule below to my AngularJS protractor test and it does not seem to be getting picked up. The api call goes straight to the real api. Also when I do console.log(browser) I do see:
name: 'httpBackendMock', script: [Function], args: [] }
None of the console.log statements in httpBackendMock are getting triggered.
describe('successful login', function(done) {
var httpBackendMock = function() {
angular.module('httpBackendMock', ['ngMockE2E', 'sampleapp']).run(function($httpBackend) {
var authenticated = false;
var testAccount = {
email: '[email protected]'
};
$httpBackend.whenPOST('/login').respond(function(method, url, data, headers) {
console.log(data);
console.log("executing");
return [200, {loggedIn: true}, {}];
});
$httpBackend.whenGET(/.*/).passThrough();
})
};
beforeEach(function(){
browser.addMockModule('httpBackendMock', httpBackendMock);
});
it('should login', function(done) {
console.log(httpBackendMock.toString());
console.log(browser);
var url = browser.getCurrentUrl();
element(by.id('user-input')).sendKeys('someuser');
element(by.id('password-input')).sendKeys('somepassword');
element(by.css('.btn-default')).click();
expect(browser.getCurrentUrl()).toNotBe(url);
expect(browser.getCurrentUrl()).toContain('account-page');
done();
});
});
You have to add all of your mocks before the browser.get() call:
beforeEach(function(){
browser.addMockModule('httpBackendMock', httpBackendMock);
browser.get(desiredUrl);
});
Also, note that console.log() calls inside the httpBackendMock would log into the browser's console instead of your terminal/console from where you've executed your tests. Check your browser logs and, if you see data value and "executing" printed there, you have the mocked backend in play and the problem is in the way you mock the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With