Entity Framework Mocking template with IDisposable implementation


I’m working on a C#/.NET StackOverflow clone and wanted to create mock data contexts for unit testing with Entity Framework.

I found a great T4 template that does this from Rab Hallett’s blog, but it did not include the IDisposable interfaces used by the .NET ObjectContext.

I’ve modified the template to include these additions to the generated Mock context so that I can now use the interface in a using block.

            using (var db = DataContextFactory.CreateContentContext(_isMock))
            { //actual code goes here
            }

The entire T4 template (ContentModelGenerator.Context.tt) is on my bitbucket if you want to use it.

Advertisements

About Bogdan Varlamov

A .NET Software Engineer that strongly believes technology should simplify and improve the quality of our lives instead of making them more complicated. View all posts by Bogdan Varlamov

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: