Real World Testing with Xamarin.Forms – Intro to AutoFixture


Tap on time to skip ahead


In this video we are going to explore how we can use AutoFixture to generate test data for our unit tests with our Xamarin.Forms applications. Between videos I’ve added a few new classes to our application. One of the first classes I’ve added is the Customer class. And it’s pretty straightforward you’ve probably done this yourself if not with Customer with Client or Person etc where we have a FirstName, LastName, Email, and PhoneNumber. And then we also have a service called CustomerService that returns a list of Customers. And then lastly we have a CustomersViewModel which is a ViewModel that a list of Customers might reference. And we have a CustomerViewModel for when we might be looking at the details of a particular customer and we might want to edit the details of our Customer.


So if we take a look at this CustomerViewModelTest that I started. One of the things I’d like to do is at the detail level of a particular entity, in this case a Customer, I would like it where whenever I set the FirstName property of our ViewModel I would like it to auto update the model that the ViewModel wraps or represent. So for example, I’m going to say “var vmUT” and again I use the “UT” suffix to indicate this is the ViewModel under test. I’m going to say “new CustomerViewModel().” So our Intellisense will give this immediate error that says “Hey CustomerViewModel is expecting an instance of Customer.” And I can easily to fulfill this by just saying “new Customer()” like so. And we will probably need to reference our models project.


And kind of one the problems with this is if I want to say “vmUT.FirstName.ShouldEqual” – I’m gonna change this new customers to be a local variable. We can say “vmUT.ShouldEqual(testCustomer)” We will get a passing test which we really don’t want. Right. Because we want all of our tests to fail for anything. Because we want our tests to fail first and then we write code to make them pass.


So, you know, one of the things I can do is like and obviously say “FirstName = Ben”. And now if I run this. It should fail. And it does. Here. But you know if I’m one to test multiple properties where it can get very tedious to constantly have to write out all the properties. So with this in mind, one of the things that right out the gate that AutoFixture can provide to us is a way to more quickly generate instances of our classes for testing.


So I’m going to create a field here. And it’s going to be a Fixture. I’m going to call it DataFixture. I’m going to say “new Fixture()” like so. And now I can replace these, you know, three lines of code and say “DataFixture.Create<Customer>().” And just like that I have a test Customer. And we will see that AutoFixture is auto populating the FirstName property of our Customers. And to better demonstrate this what I can do is set this as our startup project. And then I can debug it. And when we look at our break point here. We will see email is automatically auto populated. FirstName is auto populated. Etc. So this is really handy. And in our particular case, when we want to set the property of our ViewModel that it updates the backing model correctly. So I can say “vmUT.FirstName = DataFixture.Create<string>().” So as you can see this also works with C# primitives as well.


And I’ll write my assertion. “FirstName.Should Equal(TestCustomer)” actually what I should say is “TestCustomer.FirstName.ShouldEqual(vmUT.FirstName)”. And when we run this again you should get a failing test. And we do. Right here. So let’s make this pass. So let’s jump into the property of our ViewModel you’ll see in the constructor of CustomerViewModel and setting a local reference to model. So lets update our getter to return whatever the model has for FirstName. And then we can update the setter as well. And so now if I go back, to my CustomerViewModel test. Oops don’t need a “return” there. Silly me. And now when we run this, it passes. And we can also double check that our getter. I can say “vmUT.FirstName.ShouldEqual(testCustomer.FirstName)” And here again it should pass. So AutoFixture is really handy for whenever you want to quickly create a test instance of your particular model.

Additional Info

Register to get access to additional resources and info.