Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am new to unit testing. After going through the internet I decided to use rhino mock for unit testing. I am using repository and uow pattern in my project. Now let me start from my controller class. I have like this
C#
private readonly IRoomTypeService _roomTypeService;

       public RoomTypeController(IRoomTypeService roomTypeService)
       {
           this._roomTypeService = roomTypeService;
       }

       public RoomTypeController()
       {

       }


Now my service interface class is like below
C#
 public interface IRoomType : IEntityRepository<RoomType>
    {
    }

Repository class something like this
 public class RoomTypeRepository : EntityRepositoryBase<RoomType>, IRoomType
    {
        public RoomTypeRepository(IDBFactory databaseFactory)
            : base(databaseFactory)
        {
        }


Everything works fine when I run the application but while doing unit test using rhino mocks I couldn't get data nor save data into database.

C#
private IRoomType roomTypeRepositoryMock { get; set; }
       private IUnitOfWork uowRepositoryMock { get; set; }

       [TestInitialize]
       public void Initialize()
       {
           roomTypeRepositoryMock = MockRepository.GenerateMock<IRoomType>();
           uowRepositoryMock = MockRepository.GenerateMock<IUnitOfWork>();
       }


       [TestMethod]
       public void InsertRoomType()
       {


           RoomType roomType = new RoomType()
           {
               RoomType1="R1",
               NoofBeds=2,
               IsTVAvailable=false,
               IsACAvailable=false,
               OtherFacilitiesAvailable="NA"
           };

           var roomTypeService = CreateRoomTypeService();
           roomTypeService.CreateRoomType(roomType);

       }

       private RoomTypeService CreateRoomTypeService()
       {
           return new RoomTypeService(roomTypeRepositoryMock, uowRepositoryMock);
       }
   }

With basic ideas on unit testing I have done this code. Could any one help me to solve this issue.
Posted
Updated 12-Jan-15 2:45am
v4
Comments
George Swan 13-Jan-15 2:22am    
As it stands, your test does not actually test anything. You need to Assert (test) that some value is correct or that a certain method has been called. Unit tests should run entirely in memory and not rely on external services such as a database. A useful technique is to fake the database and to test that the values passed to it are correct. In effect, you are testing how your program relates to the database rather than testing the database itself. It’s good to keep the statements simple so as not to introduce bugs into the test.

1 solution

The problem is you didn't specify what the mocks should do. The purpose of a mock in a unit test is to provide a known environment for the test. The point is you need invariant inputs so you can then compare the outputs to your expectations.

Let's assume you want to test the CreateRoomType() method of RoomTypeService. I will simplify your code a bit here. But you didn't show the relevant parts there anyway:
C#
public interface IRoomTypeRepository
{
    void Save(RoomType roomType);
}

public class RoomTypeService
{
    private readonly IRoomTypeRepository _repository;

    public RoomTypeService(IRoomTypeRepository repository)
    {
        _repository = repository;
    }

    public void CreateRoomType(RoomType roomType)
    {
        _repository.Save(roomType);
    }
}

In order to test the method we need to supply the repository instance. This will be the mock of course, you figured that much by yourself already. Now think of what are you trying to achieve, what's the objective of your test? We need to check that the roomType instance get's saved to the repository. We need to prepare the mock in such way that we can verify this was done. I like to write my test in following format:
C#
[TestMethod]
public void CreateRoomType()
{
    // arrange
    var repository = MockRepository.GenerateMock<IRoomTypeRepository>();
    var roomType = new RoomType();
    repository.Expect(x => x.Save(roomType));
    var service = new RoomTypeService(repository);

    // act
    service.CreateRoomType(roomType);

    // assert
    repository.VerifyAllExpectations();
}

Note the Expect method call on the repository. This way we let rhino mocks know we want to check the call to the Save() method (with the exact instance of roomType). We then evaluate this expectations at the end when calling VerifyAllExpectations() on the repository mock. Now try to comment out the call to _repository.Save() in the service class - the test should fail.

This is of course trivial. But imagine there is a lot more logic in the service. Generally you only want to test one functionality at a time. This means limiting the expectations and using stubs for other method calls. If you'd use expectations everywhere your test would be too tightly coupled to the internals of the tested class hence easy to break and hard to maintain. In my experience this is especially true when you are mocking unit of work.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900