Real World Testing with Xamarin.Forms – AutoFixture and FakeItEasy

In this lesson

  • Create fake service
  • Configure fake service to return AutoFixture generated models

Transcript

Tap on time to skip ahead

00:00

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.

00:33

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)”.

02:04

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.