Unit Testing functions within repository interfaces - ASP.net MVC3 & Moq

Posted by RawryLions on Stack Overflow See other posts from Stack Overflow or by RawryLions
Published on 2012-04-10T10:53:00Z Indexed on 2012/04/10 11:29 UTC
Read the original article Hit count: 312

I'm getting into writing unit testing and have implemented a nice repository pattern/moq to allow me to test my functions without using "real" data. So far so good.. However..

In my repository interface for "Posts" IPostRepository I have a function: Post getPostByID(int id); I want to be able to test this from my Test class but cannot work out how.

So far I am using this pattern for my tests:

[SetUp]
public void Setup()
{
    mock = new Mock<IPostRepository>();
}

[Test]
public void someTest()
{
    populate(10);  //This populates the mock with 10 fake entries

    //do test here
}

In my function "someTest" I want to be able to call/test the function GetPostById. I can find the function with mock.object.getpostbyid but the "object" is null.

Any help would be appreciated :)

iPostRepository:

public interface IPostRepository
{
    IQueryable<Post> Posts {get;}

    void SavePost(Post post);
    Post getPostByID(int id);
}

© Stack Overflow or respective owner

Related posts about asp.net-mvc-3

Related posts about unit-testing