In this lesson
- Create fake service
- Configure fake service to return AutoFixture generated models
Tap on time to skip ahead
Another scenario that we can use AutoFixture in is when we want to test instances of how our ViewModels interact with fake services. So for example if I take a look at ICustomerService we will see that it has a method called “GetCustomers.” And our CustomersViewModel will be using this customer service whenever a view wants to get a list of customers to display.
So if we go into our CustomersViewModelTests you’ll see that I’ve already created a fake ICustomerService. I’ve already passed it into our ViewModel under test. So what I can do now is again I can bring in AutoFixture. And I say “new Fixture()” And I can say now “var testCustomers = DataFixture.CreateMany<Customer>().” Then “toList().” And I will probably have to import Linq here. So now I can tell our fakeCustomerService to return this list of test data. “Returns testCustomers” And now I can say “vmUT.Customers.ShouldNotBeNull()”. And we should say “vmUT.Customers.Count.ShouldEqual(TestCustomers.Count)”.
And of course we want this to fail first. And then we are going to go into our VM and say “this.service = customerService.” Create the field. And then in our getter all I have to do is say. “service.getCustomers().” And now when I run our tests they should pass.So as you can see AutoFixture is a great way to quickly create test data for your unit tests without bogging yourself down with having to write a bunch of code that generates test code which in itself could have bugs and screw up your unit tests. So AutoFixture is a big valuable tool in my test driven development tool belt.
In this series, Ben Bishop will cover the following: