Ask a questionAsk a question
 

AnswerEF and Moq? Virtual methods required...

  • Tuesday, November 03, 2009 4:39 PMbenwalker Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I've been using EF with MVC to great success so far, and now need to put in some unit tests for another application that uses the same data framework.

    As far as unit testing goes, we've opted for NUnit and Moq, however the problem comes with Moq's requirement that everything needs to be virtual. For example, in our other application, we've a simple line of code:

    user.AddressReference.Load();
    if (user.Address != null) user.Address.CountryReference.Load();
    
    
    ... so figured that to mock the user object we could use

    private User CreateMockUser()
    {
          var user = new Mock<User>();
          user.Setup(x => x.AddressReference.Load()).Raises(x => x.Address = null); // Should be *new* address
          return user.Object;
    }
    
    
    However since all the methods and properties generated by entity framework are not virtual, is there any way we can accomplish the above without trying to add an interface?

    Suggestions and thoughts most welcome at the moment.

    Thanks.
    a user with Address

Answers

All Replies