When you create a mock using Mock.Create<> or Mock.Create there are three Behavior modes available. Each one causes the mock to behave in very different ways. For those of you new to mocking, you may not know at first what each of these behaviors means; so let’s take a look!  

In these examples I will be testing a method that retrieves a UserProfile, it looks like this:

public UserProfile GetUserProfile(User user)
 bool foundUser = false;
 using (var connection = new SqlConnection(connectionString))
 using (var command = new SqlCommand("SELECT * FROM Users WHERE UserName =@userName'", connection))
                  command.Parameters.AddWithValue("@userName", user.UserName);
                  var reader = command.ExecuteReader();
                  foundUser = reader.HasRows;
 if (foundUser)
                      UserProfile userProfile = new UserProfile();
                      userProfile.UserId = reader.GetGuid(0);
 return userProfile;
 return null;


The test looks like this:

 string connectionString = "database=;uid=;pwd=";
 //This is the line we will be changing for each example in this blog
          var mockedDataReader = Mock.Create<SqlDataReader>(Behavior.CallOriginal);
          Mock.Arrange(() => mockedDataReader.HasRows)
 //Here we use some profiler "magic" to swap out the command that is instantiated in the method under test.  We will go over this in a later blog
          var sqlCommand = new SqlCommand();
          Mock.Arrange(() => sqlCommand.ExecuteReader())
          var userRepository = new UserRepository(connectionString);
          var userProfile = userRepository.GetUserProfile(new User());

For this blog the only line of code I changed in each scenario is the creation of the mock DataReader in the test. 



This will instantly fail a test if an unarranged method is called.

var mockedDataReader = Mock.Create<SqlDataReader>(Behavior.Strict);


As you can see, JustMock fails the test because there was a call to GetGuid(0), which was not arranged.

Behavior.Loose (default)

This will tell the mock to act like a stub, and will automatically return a default value from methods or properties that have not been arranged.

var mockedDataReader = Mock.Create<SqlDataReader>(Behavior.Loose);


var mockedDataReader = Mock.Create<SqlDataReader>();


JustMock returned default(Guid); from reader.GetGuid(0),  I never arranged for it do that, it does it automatically to keep the test running. 


This tells the mock to call the member’s original implementation if it has not been arranged.  


var mockedDataReader = Mock.Create<SqlDataReader>(Behavior.CallOriginal);


As you can see, this time there was an InvalidOperation exception because the actual implementation of GetGuid(0) on the SqlDataReader was called, and there is no data in the data reader. 

I hope this helps understand the various behaviors you have available when working with mocks in JustMock.  As always feel free to ask any questions :) 

Happy coding!


Comments are disabled in preview mode.