[SOUND] Okay,
so we're back in our code editor and I'm located in lecture 45 folder,
which is located in full stack dash course five examples folder.
And here, I opened up the readme.txt that tells us that code for
the entire application code that we're actually going to be testing
is copied directly from Lecture 25.
So you can look at that lecture if you want to see
it running in the original state.
Here we're going to take a look as to what that code does.
And basically it has a controller here that uses this menu category service.
And this menu category service reaches out to this API base which is this
URL right here.
If we scroll down you'll see this menu category service.
And basically it allows us to pull categories of different items within
the restaurant menu and we use this API base path because we injected
together with our $http service, into our MenuCategoriesService.
So that in fact is the service we want to test, and for this short lecture we're
just going to test one method which is getMenuCategories.
And what we want to get out of it is to make sure that we set it up properly such
that we passed back the response that is returned with the data from the server.
So what we will going to do this, let's go to menucategories.service.spec.js.
That's the file that we are writing our test in.
And let's take a look as to how we set it up.
The first thing we're doing here is we're calling the beforeEach and
we're injecting our module, our application module, MenuCategoriesApp.
That's the same one that came from
the main one from the application we're testing.
The next we're going to do is inject the dollar sign injector service.
Using that service, we can now call the get method and
literally pull out all the things we want to pull out from the angular context.
And one of them is the MenuCategoriesService itself that
we're testing.
Notice we're just saving it off as a variable Inside our describe,
that we can then reference inside of our it test method.
Notice that we also are not injecting anything manually inside
the MenuCategoriesService, even though it requires some injections.
We don't need to that.
Angle will take care of that for us automatically.
We also need the $HTTP back end so we can use that to mock out our requests.
And we also need the API base path and
that's what was defined right here as a constant because we need to reuse that
when we make our fake or mocked out network calls.
Once we expose those three things outside of our before each we
can then use it inside the it, the test method.
You can see herethe HTTP backend,
we're using the whenGET and we're basically compiling this URL.
We're saying ApiBasePath + the slash categories, and
if you take a look into our app and
scroll down to the actual method we're testing right here, getMenuCategories.
It is the API base path + /categories.json
which is exactly what we're doing here, /categories.json.
And then we're telling it when that call gets made, go ahead and intercept it and
respond with an array that just has two things, lunch and dessert strings.
That's really all we want.
We want to make sure it's set up properly.
Once we set this up, we can then called menucategories.getMenuCategories and
we know that that returns a promise because the htp server returns a promise.
And we're saying that we're going to catch that promise in the then function, and
we're going to grab the response.
And the response is response.data.
The data of the response is actually sitting inside the data property.
And depending, of course, on your response,
it might be a little bit different.
You might be just passing the data directly out of the getMenuCategories.
But in our case, we'd pass the entire response out.
That might have been a mistake, but it is what it is now.
Okay, so, we're checking now and expecting that response.data is equal to
what we basically told it to send back, which is lunch and dessert as an array.
And then, of course, we want to make sure not to forget since this is
an asynchronous call, want it go ahead and flush it right away, so
basically it will behave like a synchronous call.
So, let's save it.
Let's go back to our browser.
And let's click, I already started the browser sync.
So let's click on our test runner.
And you can see, should return categories list, is executing properly,
and we're seeing, zero failures so we're all good to go.