-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to mock Http requests in Protractor tests #412
Comments
This is how I do it in my tests:
|
Thanks @alvipeo I'm trying it now, just struggling to get my dataService (your dirItemSrv) into the test. I'll keep trying and let you know how I get on. |
hi @daddywoodland @alvipeo |
Hi @samvloeberghs, The terminology does trigger some interesting conversations. As you say it's a unit test but not of a service, I'm testing a unit of behaviour within my UI (port level testing). Protractor is my mechanism for accessing the closed system to test a behaviour, and since Protractor is always labelled as an end to end testing tool, I mistakenly used the term. Either way, I'm after a Protractor, not Karma solution to this problem :o) Cheers, |
I actually do integration tests, not unit tests (well, rarely I do them too). That's the way that works best for me. So Karma is in my case. |
I've been looking at this on and off for the last few days without making much progress. I think the technique suggested above (and everywhere else on t'internet) involves configuring the injection before the component/service is initialised. However as protractor is running tests against an app that's already running it's not working. I'm going to look at initialising the application with the mock backend in place so that I can configure mocked responses on a test by test basis. |
In the past I've done this by mocking my backend with a node-red implementation. By setting a flag that was then triggering the initialisation of this mocked API and redirecting/proxying the API address to that mocked API. But that said: When doing E2E tests you should keep the mocking to the minimum, trying to avoid it completely. E2E testing explicitly means that you test end to end, real app till real backend. If you don't want to polute your production DB for example, you should have a staging or dev server running next to your UAT or prod environment, where you test against regressions. Perhaps use docker to instantiate a setup on the fly, .. |
To elaborate on your last comment: It's indeed hard and probably unneeded to change the app implementation on runtime. What you could do is change the environment. While running your protractor config, proxy the requests and point them to the mocked API -> run protractor, set flag Just some quick thoughts :) |
Thanks @samvloeberghs I'll take that into account when trying out mocking the HTTP response. I was looking at json-server as a possible solution for mocking the API. Regarding the reference to 'E2E', these aren't end to end tests but unit tests of the behaviour of the front end as a closed component. I've seen it called 'port level testing', 'sociable unit tests' and 'behavioural unit testing'. Ultimately I need to mock any requests that move across the boundary of my component, in this case calls the the API. If you're unfamiliar and are interested in the idea, the approach was triggered from a talk by Ian Cooper I saw a couple of years ago. I didn't see this one at NDC 2013 but it's the same presentation I believe. Cheers! |
sorry this may sound super rule but I'm closing long standing issues and refactoring the codebase. If you run into this problem again feel free to open another issue |
Hi,
I'd like to mock the response from an API in my end to end tests but I'm not sure how to include and use MockBackend in the project.
Can you offer any advice?
Cheers,
The text was updated successfully, but these errors were encountered: