This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Using ngMockE2E to mock backend data

4:02 Angular 1.x lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

Avatar
Quintin West

From what I can tell it doesn't work in Ng 1.3.x; Not sure though, please advise.

In reply to egghead.io
Avatar
Ben

The JSBin example is using 1.3.15

In reply to Quintin West
Avatar
Joseph

Do you know how to mock in Protractor without needing to modify the app's source?

Avatar
Ben

Hey Joseph,

I guess you could check the URL and/or port the app is running on, and conditionally define the mocks if it matches your test environment.

HTH,
Ben

In reply to Joseph
Avatar
Victor Alfonso Hazbun Anuff

Why you don't create a video of how to use protractor and ngMockE2E?

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.

If we go ahead and add the angular mocks JavaScript file and then add the ngMockE2E module to our app definition, if we reload this and then try and load one of these, you can see that it's going to error out on this. It's saying that the http back end module expected a request and didn't get it.

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.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?