Testing LINQ to SQL repositories with Rhino Mocks

13 Oct

Today I was writing some unit tests for a repository based on the Nerd Dinner example of a LNIQ to SQL class repository. I had a Customer table in the database that had a auto increment identification column. I needed to Mock the standard LINQ to SQL functionality where the newly assigned ID is set when the SubmitChanges method has been called and the row is committed to the database.

This is easy to do by Mocking my CustomerRepository interface and using Rhino Mocks Callback method.

Based on the Nerd Dinner example I have a customer repository interface below.

public interface ICustomerRepository
{
    IQueryable GetAllCustomers();
    Customer GetCustomr(int id);
    void Add(Customer customer);
    void Delete(Customer customer);
    void Save();
}

To Mock this interface I have used a generic class factory developed by a colleague of mine that can dispense a Mock object into my customer controller with ease.

public TestCustomerController()
{
    _customerRepository = _mock.StrictMock();
    //Set up the class factory to dispense the mock objects
    GenericClassFactory.Dispenser = o => _customerRepository;
    _controller = new CustomerIdentityController();
}

Inside my controller I use the generic class factory to dispense, by default, real object or, if the dispenser is set, my mock object. This is done using the code below.

ICustomerRepository _customerRepository = GenericClassFactory.Get()

Finally I wrote my test method using Rhino Mocks. The create customer method creates a new customer object and adds it using the LINQ to SQL add method. I use the Rhino Mocks call back method to get and instance of the new client that was created by my CustomerController. After the client is added the client controller and LINQ to SQL verifies that there are no errors, which allows me to commit the new record using my repositories save method. This method uses the Callback method to to mimic the LINQ to SQL functionality by assigning a customer ID to the to the customer object which is then returned by my controller.

[TestMethod]
public void CreateCustomer_ValidData_ID_Expected()
{
    Customer customer = null;
    _customerRepository.Expect(o => o.Add(Arg.Is.NotNull))
                       .Callback((Customer c) =>
                                      {
                                          customer = c;
                                          return true;
                                      });
    _customerRepository.Expect(o => o.Save())
                       .Callback(() =>
                                      {
                                          customer.ID = 1;
                                          return true;
                                      });
    _mock.ReplayAll();
    var customerId = _controller.CreateCustomer("FirstName", "Surname");
    Assert.AreEqual(1, customerId);
}

This is nothing new but is a great way of mocking a method that does more then just return a value.

 
Comments Off

Posted in C#

 

Tags: , ,

Comments are closed.