The official source of information on Managed Providers, DataSet & Entity Framework from Microsoft
The information in this post is out of date.
Visit msdn.com/data/ef for the latest information on current and past releases of EF.
Writing unit tests is a core practice in the vast majority of modern software development approaches. There are several benefits to it, but the one I personally tend to value the most is how I can take a component with enough unit test coverage and start refactoring it without worrying that I could accidentally introduce regressions.
With Test Driven Development and Unit Testing in general, it is critical that unit tests are lightning fast: in practice, you should run all tests for a component after applying each refactoring step, and that should not affect your rhythm. Whenever you feel the urge to skip the test runs, it is time to streamline your tests.
A well known way to accomplish this level of swiftness is to substitute the actual dependencies of the component being tested with test doubles. Test doubles expose the same API surface as the dependencies they impersonate, but instead of trying to emulate the often complex behavior of the real thing, they can limit themselves to provide canned responses or record the inputs received during the execution of the tests (which can help in verification even more).
When applied to the testing of the domain logic in an application using some O/RM technology, test doubles should ideally substitute the persistence layer. That way, unit tests typically never invoke the actual persistence framework, and more important, never in fact hit the database.
During the development Entity Framework 4.0, we spent a lot of time thinking how to make it easier to substitute Entity Framework in your unit tests. Here are a few improvements that we believe will make a difference on that front:
a. Support for more LINQ operators, such as Contains, DefaultIfEmtpty, and improved support for Single/SingleOrDefault.
b. More translations for common Base Class Library patterns including various Math class methods, Guid.NewGuid, etc (some of this work is not included in Beta1).
c. Improved preservation of nested OrderBy operations.
d. Several bug fixes that further align the behavior of LINQ to Entities with LINQ to Objects.
The following sample code shows a very simple way to create and use a fake implementation of an ObjectContext that exposes properties of type IObjectSet<T>:
/// <summary> /// Data access context interface for SimpleBlogging /// </summary> public interface
ISimpleBlogging
:
IDisposable
{
IObjectSet<Blog
> Blogs {
get
; }
IObjectSet<Post
> Posts {
; } } /// <summary> /// Fake data access context for SimpleBlogging /// </summary> public class
SimpleBloggingFakeContext : ISimpleBlogging
{ private
FakeObjectSet<Blog
> _blogs; public
{ return _blogs ?? (_blogs = new
>()); } } private
FakeObjectSet<Post
> _posts; public
> Posts { get { return _posts ?? (_posts = new
>()); } } public void Dispose() { } } /// <summary> /// Utility factory method for contexts /// </summary> private static
GetContext() {
context = new
SimpleBloggingFakeContext
(); InitializeWithCannedData(context); return context; } [
TestMethod
] public void TestAddPost() { using (
context = GetContext()) { var blogging = new BloggingService(context); var newPost = blogging.AddPost( "Diego", "Hiatus", "This is my last post for a while."); var blog = context.Blogs.Single(b => b.ID == "Diego");
Assert
.AreSame(newPost.Blog, blog); } }
Stay tuned for a more in depth discussion of testability improvements, as well as other topics mentioned in this post.
Thanks,
Diego Vega Program Manager, Entity Framework