2
votes

Can anybody provide a good starting point or example of using Moq and nUnit to perform tests against the entity framework inside MVC. I have a DomainModel which contains "MyModel.edmx" which contains a table "Posts". I want to perform a test populating a fake repository of this.

I have been following this: http://blogs.msdn.com/b/adonet/archive/2009/12/17/walkthrough-test-driven-development-with-the-entity-framework-4-0.aspx

But I am unsure how to use Moq/nUnit instead of the inbuilt tests

1
Is there anything special about EF in MVC3?Daniel Hilgarth

1 Answers

1
votes

This is another way of creating Moq Object (for testing purpose) assuming you implemented a datarepository.

public static class UnitTestHelpers
        {
            public static MyModelRepository MockMyModelRepository(params Posts[] post)
            {
                // Generate an implementer of MyModelRepository at runtime using Moq
                var mockPosts = new Mock<MyModelRepository>();
                mockPosts.Setup(x => x.Posts).Returns(post.AsQueryable());
                return mockPosts.Object;
            }
    }