Protractor Tests without a Backend (or how to mock a backend)
See original GitHub issueHi!
I want to write integration tests for an angular app with protractor. As @juliemr mentioned in a comment to a similar issue written by @mrappleton, Protractor is intended for End to End Tests with an actual backend hooked up. This is a reasonable use case and good to know.
However in my case I have a fairly well tested Backend. Therefore I would prefer to have my protractor tests not depend on my backend.
The advantages of this could be
- Testing the angularjs app can be done in separation of the server
- I won’t test things twice
- Less dependencies to worry about
I played around with something similar to @leifhanack backend dsl. It is possible to test the whole App and Mock the Api Routes in the Test. However this feels like a lot of overhead (setup rest server in each test, worry about body parsers,…)
In my App I have a Api Module that talks to the server. I would really like to spy and mock this Api Module from the tests. This is difficult as the tests (protractor) are not executed in the same scope as the app (selenium driven browser).
I would fancy to do something like this in my tests:
Api = $injector.get('Api');
sinon.mock(Api, 'getSomethingFromServer').andRespondWith({foo: 'bar'})
// do tests, go to page, fill out form, submit form, etc...
assert(Api.getSomethingFromServer.wasCalledOnce);
In this case I could test the client in seperation without doing a lot of boilerplate to get up running.
I know that this is not possible right now, because $injector
is running in the browsers scope.
The only way to do something similar would be to use ptor.addMockModule
.
This is limited, as it is only possible to replace whole modules (and not mock parts of it) and it is difficult to make assertions on the mocked functions (something like mockedModule.function.wasCalledOnce
)
What would be a great way to mock the access to the backend to test an angular app in isolation?
What is the angularjs teams view on this issue?
Issue Analytics
- State:
- Created 10 years ago
- Reactions:4
- Comments:44 (2 by maintainers)
Top GitHub Comments
Guys, to you really run all the edge cases with real backend? Mocks are a must!
2016-04-13 11:27 GMT+02:00 Nabil Boag notifications@github.com:
I think I’m doing something similar to what you’re wanting to setup, don’t remember where I read about it but here are the steps:
I have a mocked backend module
mocked-backend.js
withwhere the
mainApp
is name of the real apps, in which all the routes are declared. Notice the dependency tongMockE2E
, you will need to have reference to angular-mocks.js in your index.html file for this to work.In my protractor scenario file:
And in your test in your beforeEach block you could put
The only I couldn’t figure out was setting response header with
Set-Cookies
so that the cookie part of my app could be tested. I would like to hear some insight about it as well.