From the course: ASP.NET Core: Test-Driven Development

Test N+1 case - ASP.NET Core Tutorial

From the course: ASP.NET Core: Test-Driven Development

Start my 1-month free trial

Test N+1 case

- [Instructor] If you're following along with the code, I've modified some of the test code a little bit to make it easier to work with. So you may want to start with the example files from the begin state of this video. So now we have our test for our smoking and non-smoking case, let's tackle the criteria for if the guest is bringing pets. Let's identify our base case. I think it would be that if the guest is bringing pets, and the room doesn't allow pets, then the booking will be invalid. We need a way to express that in our tests. We know that our service is getting its data from the rooms repository, and because data retrieval is not our class's responsibility, we can mock that out. To create a mock, we'll be using Moq, or mock. There are many different libraries and packages out there, or you can even just provide your own stub implementations for your dependencies. But I find Moq particularly easy to understand and use. We're going to start by using NuGet. We'll right-click on our project, select manage NuGet packages, click the browse tab, and search for Moq. Choose the package, and press install. We'll choose OK to the changes. Now it's installed. We can close the tab, and go back to our test. Now we're ready to define our mock. We'll create a new private mock, type IRoomsRepository, and call it roomRepo. So the way xUnit works is the constructor of the class will be called before every test, so let's create a new constructor, and we're going to initialize our roomRepo mock to be a new mock of IRoosmRepository. We're going to make the subject method not static, and we're going to pass in our roomRepo instance.object, which is going to give us an IRoomsRepository. Now we're ready to add tests. We're going to add a new test by copying this one. The case is the booking has requested pets, the room is not allowing pets, not allowed, and the state is invalid. Now let's set up our mock. We're going to call our mock.setup, and this is going to allow us to say when our roomRepo is called GetRoom with a certain value, in this case, ID1. We want it to return a new room, and it's going to not allow pets. We're going to change our test value here, so our booking is requesting HasPets true, and our assertion is going to remain the same, that our booking is invalid. Now we can run our test. And the test failed as expected. And let's go into our code and we'll make our test pass. So we're going to grab the room information from the rooms repository, getRoom with the room ID that we're passing into the test. And now we're going to check if the booking has requested pets and the room does not allow pets. Then, we want to return false. We'll save that, and run our tests again. They pass as expected. So now if we go back to our tests, we can see that this test is one of four cases. We want to test if the guest is bringing pets, and the room disallows or allows pets, and we also want to test if the guest isn't bringing pets, and the room allows and disallows pets. They can get pretty tedious, so I've created a snippet here which tests all of those cases and I can paste that in. Now, we can run our tests again, and our code just happens to pass all of these tests. Now we're at a good stage to do some refactoring. We'll tackle that in the next video.

Contents