Mocking backend responses is extremely simple with angular-mocks.js and the ngMockE2E module. This lesson will show you how to include and use the module, ensuring you can stay productive on the front end even if your back end is broken, slow, or doesn't even exist yet.
In this lesson, we'll see how to use the ngMockE2E service to intercept remote calls and respond with your own data, which can be really useful when your back end is either broken or not ready yet or any number of other use cases.
We've got our example here where we've got three buttons that will go and fetch some data from the Star Wars API.
The first thing that we need to do is actually inject the http back end module which is actually what http uses behind the scenes. When you're using the angular mocks file, you get a mocking version of it.
If we inject that, we can then put this definition in here that says http back end, when you get a get request to this URL here, we want you to respond with this data object.
You can see here that we're just providing an object with a name property. It's actually the object that you would have in result.data. You're not redoing the entire result object itself.
If we then save this and run it, you can see that we then send back our object which is Jean-Luc Picard but no remote call was actually made. If we request any of these other URLs, we're still going to get this error saying that we requested something that it didn't expect.
I'm going to simplify this and use this whenGET method. They have shorthand methods that are like whenGET, whenPOST just so you can simplify the arguments there.
Now, we're going to look at another way that you can call whenGET which is to pass a regular expression instead of an actual string. Obviously, that gets you some flexibility. What we're going to do here, we're going to do a wildcard regex. This is going to accept everything.
When we get to that part, we're going to call passThrough instead of respond. Instead of sending back data, we're going to say, if the request matches our regular expression, just go ahead and pass it through and let it actually make that remote XHR call.
If we then come back here, we can see that our other ones do in fact call out to the remote server and bring back the data. Our one that we specifically handled will use our fake data.
Now, there's one more way to call these when functions which is to pass a function. That function is going to receive the URL that was requested. Then if you return true, then it will use your respond or your passThrough call.
If you return false, it's going to fall through to the next definition. In ours, we're going to take the second last character in the URL, so this number here. We're going to turn it into a number with this plus sign. We're basically just going to say if it's one, respond with Buzz Lightyear.
To recap, if the second last character is a one, we're going to respond with Buzz Lightyear. If the request is the specific URL, we're going to respond with Jean-Luc Picard. Everything else is going to get passed through to the real service that we'll call the XHR.
If we're going to run this, we can see that the Luke Skywalker button returns Buzz Lightyear and does not make a remote call. The C3PO button returns Jean-Luc Picard and does not make a remote call. The R2D2 button, in fact, does make the remote call and load the real data.