3

I am exploring CodeCamper project by JohnPapa on github https://github.com/johnpapa/CodeCamper. This is a ASP.Net SPA application and I am also working on similar project.

I interested to write some UnitTests for WebAPI controller. Controller contractor requires UnitofWork instanse and UnitofWork is initiate in Application_Start method.

When I run my UnitTest project UnitofWork object is null. How I can initiate UnitofWork from UnitTest project so that I can run my test methods. I hope make myself clear.

Here is a sample UnitTest method for following controller.

LookupsController.cs

UserControllerTest.cs

    [TestClass]
    public class UserControllerTest : ApiBaseController
    {
        [TestMethod]
        public void GetRoomsTest()
        {
            var controller = new LookupsController(Uow);
            var result = controller. GetRooms().Any();
            Assert.IsTrue(result);
        }
    }

Again why Uow is null? What should I do, so that I can write unit test methods for this type of project/architecture.

For more detail about code you can check github repo.https://github.com/johnpapa/CodeCamper

1
  • You can probably use a Mock framework like Moq to create a new mock instance for the UoW interface, and pass that mock instance to the controller Commented Sep 19, 2013 at 14:33

1 Answer 1

3

Use any mocking framework to create a fake/stub/mock for ICodeCamperUow (below I am using NSubstitute):

[TestMethod]
public void GetRoomsTest()
{
  // UOW we need to use
  var fakeUOW = Substitute.For<ICodeCamperUow>();

  // setting up room repository so that it returns a collection of one room
  var fakeRooms = Substitute.For<IRepository<Room>>();
  var fakeRoomsQueryable = new[]{new Room()}.AsQueryable();
  fakeRooms.GetAll<Room>().Returns(fakeRoomsQueryable);

  // connect UOW with room repository
  fakeUOW.Rooms.Returns(fakeRooms);

  var controller = new LookupsController(fakeUOW);
  var result = controller.GetRooms().Any();
  Assert.IsTrue(result);
}

Please consider reading The Art of Unit Testing which is a great book to learn about unit testing.

Sign up to request clarification or add additional context in comments.

8 Comments

Now I have instance of UnitOfWork that's good but still Uow.Rooms always returns zero(0). But when I use same method via application it return me all room. Why?
@MohsinJK The thing with fake objects: You can set them up any way you want but you need to set them up. Only you know how.
Not exactly i am still not able to add a object in fake Uow.
What and where exactly do you want to add?
In my project I also have Post method for adding new object. I am using that object for insert one object in memory but I can't add the fake Uow of is still empty. I don't know how can I add object in memory for unit testing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.