unit testing ef datamodel
I'm working on a personal c# project in my spare time. Before this projected I haven't really done any unit testing, bu开发者_JAVA百科t I that it was time to learn so I read a couple of tutorials/blogs and installed NUnit and Testdriven.Net in VS2010 and I think I got the basics covered now.
My project uses a data model, which I created using EF4. I've also created a repository to retrieve the data and now I want to test that repository. How should I test it? Can I somehow avoid making calls to the database everytime I want to test a method in the Repository?
cheers
define an IRepository interface. have a real implementation of it that uses the database. have a fake implementation of it that returns dummy objects for the purpose of unit testing
You can also use mocking frameworks to create "fake" versions of your repositories. Moq is one I use very often. Essentially, you write code to fake return values of your repositories based on configuration...
var mock = new Mock<YourObject>();
mock.Setup(m => m.DoSomething().Returns(true));
var result = mock.Object.DoSomething();
Assert.IsTrue(result);
Here's a good tutorial on getting started with Moq by Stephen Walther.
精彩评论