Asserting Method Call with FakeItEasy

In this lesson

  • Use MustHaveHappened to confirm method call
  • Update assertion to check passed parameters


Tap on time to skip ahead


In our previous video, we added FakeItEasy to our project so we could fake out a dependency of our ViewModel’s constructor. Now, we’re going to go to the next step and use FakeItEasy to test the behavior of our ViewModel and how it interacts with other classes. Specifically in this particular instance, how our UserAuthenticationViewModel class interacts with IUserService. This kind of test is actually not a unit test but instead an integration test, because you are testing how two classes integrate with each other. Without anymore exposition, how about we dive into the code?


I’m going to create a new test method. I’m going to say, “public void TestVMCallsAuthenticateOnUserService”. One of the things I sometimes do is add underscores to help break up exactly what we are expecting with this particular test. Here, this is a bit more readable. Test_VMCalls_Authenticate_OnUserService.


One of the things we’ll want to do is create a reference to our fake service. I’m going to call this FakeUserService. I’m going to make this a field. You’ll see why here in a second. Now, let’s actually start writing our test. I’m going to say, “ViewModelUt.EmailAddress =”. Next we’re going to say, “ViewModelUt.Password = letmein”. Then, we’re going to call ViewModelUt.AuthenticateCommand.Execute. We’re just going to run this test for a second and make sure we don’t have any compile errors, which we do, because no ICommand takes zero parameters. I’m going to try running and now we have a failing test. That is because in our Authenticate command we are throwing a NotImplementedException. This is a good thing.


Let’s make this go away. I’m going to delete that. Now, when we run our test everything passes. But, we need to actually make sure our ViewModel via our command is actually interacting with our service correctly. Right off the bat, one of the things we can say is, “A.CallTo”. Then I’m going to use a lambda expression. FakeUserService.Authenticate. Then we’re going to say, “A string” for now we’re just going to say “Ignored” so it will basically be any string. MustHaveHappened. Now, when we run this our test should fail and it does.



Let’s go into our Authenticate command and let’s call UserService.Authenticate and I’m just going to put in two empty strings. Now, if we go back to our test and run it, it passes. But we want to make sure our ViewModel and our command are calling the UserService with the appropriate parameters. We’re going to say “ViewModelUt.EmailAddress ViewModelUt.Password.” We want to make sure that we’re passing appropriate parameters. Now, when I run our test, it will fail. But again, if I go into my Authenticate command and I say, “VM.EmailAddress” then say “VM.Password” and I go back and run it passes.


This is how we can very easily use FakeItEasy to make sure our classes are playing nicely with other classes. One of the reasons I like FakeItEasy is that it has a very nice fluent syntax where I can say, “A.CallTo FakeUserService.Authenticate ViewModelUt.EmailAddress, ViewModelUt.Password MustHaveHappened”. And, as I’ve discussed before, being able to read unit tests easily is a key component to making sure your tests don’t get thrown away later down the road.