简体   繁体   中英

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, but 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM