Telerik blogs

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:

[TestMethod]
 public void GETPROFILE_SHOULD_NOT_RETURN_NULL_FOR_VALID_USER()
      {
 //Arrange
 
 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)
              .Returns(true);
 
 //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())
              .Returns(mockedDataReader);
 
 //Act
          var userRepository = new UserRepository(connectionString);
          var userProfile = userRepository.GetUserProfile(new User());
 
 //Assert
          Assert.IsNotNull(userProfile);
      }

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

Behaviors

Behavior.Strict

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

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

image

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.

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

or

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

image

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

Behavior.CallOriginal

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

Example:

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

image

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

Comments are disabled in preview mode.