Integration Testing EF6 — aggressively rebuild your database for an integration test

Sometimes you need to do a really end-to-end automated test involving your Entity Framework database. In cases like this, it’s important to be able to reset the database to a known state, but this can be fraught with difficulties — apps hold onto connections, and the code for re-building the database isn’t obvious. Here’s what I’m using at the moment.

This is a really aggressive database (re)initializer for EF code-first with migrations; use it at your peril but it seems to run pretty repeatably for me. It will;

  1. Forcibly disconnect any other clients from the DB
  2. Delete the DB.
  3. Rebuild the DB with migrations and runs the Seed method
  4. Take ages! (watch the timeout limit for your test framework; a default 60 second timeout might not be enough)

Here’s the class;

public class DropCreateAndMigrateDatabaseInitializer<TContext, TMigrationsConfiguration>
    : IDatabaseInitializer<TContext> 
    where TContext: DbContext
    where TMigrationsConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<TContext>, new()
{
    public void InitializeDatabase(TContext context)
    {
        if (context.Database.Exists())
        {
            // set the database to SINGLE_USER so it can be dropped
            context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "ALTER DATABASE [" + context.Database.Connection.Database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
 
            // drop the database
            context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "USE master DROP DATABASE [" + context.Database.Connection.Database + "]");
        }
        var migrator = new MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration>();
        migrator.InitializeDatabase(context);
    }
}

Use it like this;

public static void ResetDb()
{
    // rebuild the database
    Console.WriteLine("Rebuilding the test database");
    var initializer = new DropCreateAndMigrateDatabaseInitializer<MyContext, MyEfProject.Migrations.Configuration>();
    Database.SetInitializer<MyContext>initializer);
    using (var ctx = new MyContext())
    {
        ctx.Database.Initialize(force: true);
    }
}

You should also set up your connection string in a particular way. In your integration test project,

1. set up your connection string to have “Pooling=false;” This doesn’t help the speed of the test, but helps mitigate problems with multiple tests running against the integration test db. (Thanks to Ladislav Mrnka for this.)

2. set the initial catalog to a different DB from your production one — I add ‘IntegationTests’ to the end of the name of the database. This ensures you’re not going to delete the database which is, say, underlying the web app you’re building.

<connectionStrings>
    <add name="MyContext" connectionString="Pooling=false;data source=localhost;initial catalog=MyContextIntegrationTests;[...]" providerName="System.Data.SqlClient" />

</connectionStrings>

Lastly, you’ll need to make sure that your tests run in series, not in parallel. I use NCrunch, and needed to use the NCrunch.Framework.SerialAttribute to make sure that tests don’t overlap.

Invoking the TypeScript compiler when a project builds

If you’re using TypeScript, it’s good to set up your .csproj file so that it builds all the TypeScript files and compiles them to JS files. This makes sure that when someone else checks in a .TS file, you’ll get the corresponding .JS file without having to regenerate them manually, and that means you can avoid checking the .JS files into source control.

Here’s what you need to do to set up TypeScript for both developers and your build server.

1) Install the TypeScript VS2012/2013 extension from http://www.typescriptlang.org/#Download. All your developers need to do this because the changes in this post make the TS compiler run every time the project builds.

2) If your developers or your build server use VS2012, or a mix of VS2012 and VS2013, you’ll need to edit your .csproj file to include some defaults. (We currently develop in VS2013 but the build server is VS2012.) Open the .csproj file in a text editor and insert the following segment. It goes very near the top of the file — in our project, it goes on line 5, after the <Import> elements, before all the other <PropertyGroup> elements.

<PropertyGroup Condition="!Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')">
 <!-- These are the defaults introduced in VS2013, duplicated here for VS2012 builds -->
 <TypeScriptRemoveComments>false</TypeScriptRemoveComments>
 <TypeScriptNoImplicitAny>false</TypeScriptNoImplicitAny>
 <TypeScriptNoResolve>false</TypeScriptNoResolve>
 <TypeScriptGeneratesDeclarations>false</TypeScriptGeneratesDeclarations>
 <TypeScriptModuleKind>amd</TypeScriptModuleKind>
 <TypeScriptOutFile>
 </TypeScriptOutFile>
 <TypeScriptOutDir>
 </TypeScriptOutDir>
 <TypeScriptSourceMap>true</TypeScriptSourceMap>
 <TypeScriptMapRoot>
 </TypeScriptMapRoot>
 <TypeScriptSourceRoot>
 </TypeScriptSourceRoot>
 <TypeScriptTarget>ES5</TypeScriptTarget>
 <TypeScriptAdditionalFlags>
 </TypeScriptAdditionalFlags>
 <TypeScriptEnableCompileOnSave>true</TypeScriptEnableCompileOnSave>
 </PropertyGroup>

2) Near the end, after <Import Project=”$(MSBuildBinPath)\Microsoft.CSharp.targets” />, insert this;

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" />

3) If you have a build server set up, you’ll need to install the TypeScript compiler on the build server. Otherwise, your build server is going to fail to generate the JS files. So head to your machine at either

For a VS2012 build server: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TypeScript
For a VS2013 build server: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\TypeScript

And copy the TypeScript folder to the same location on the build server. This installs the compiler into the location mentioned in the <PropertyGroup> element from Step 2.

This should do you. Save up the .csproj and reload it in Visual Studio. When you build the project, all the .JS files should be generated automatically every time you build. You may need to exclude the .JS files from source control.

StackOveflow-driven-development; ask the internet first, code second (but not really)

I’ve noticed a technique that’s been very helpful for me when writing software. Hopefully it’s a tool you might use, too.

When I come to an impasse — when I’m not quite sure what the next step in my work is — I find that I often hit a particular technical problem I can’t trivially solve. The problem that prompted this blog post was one that mashed together problems in Entity Framework, Generics, and Entity SQL. I realised this wasn’t going to be trivial, and I wasn’t sure how to proceed.

So, I hit stack overflow, clicked ‘Ask a question‘, and got to writing, I spent a while formulating and re-writing the question for the unknown programmers of the internet, trying to state the core of the problem and the way I wanted it solved. It included the problem, my approaches, my half-baked architectures.

Interesting alchemy starts to happen when you do this. You start to realise your problem in more detail. You start to see the flaws in your own thinking. You come to understand how you may have limited yourself, driving for the wrong kind of solution. When you get your own thoughts out on paper, you can see, then correct, the flaws.

So a question that starts;

“I’m having trouble querying for tables…

transmutes through

“Entity Framework doesn’t allow generics…

through

“I’m thinking of using code generation…

to

“If I can get hold of the EDMX model, I can run a code generator…

And you start to realise the shape of the problem you really help with.

In the end, stating the problem clearly for the folks on Stack Overflow (and this is a minimum standard for asking a question) is often all you need to see the solution to your own problem. So you may not need to post the question in the end. In this respect, it’s very similar to Rubber Duck Debugging, but with the additional benefit that, should you not figure it out, you can immediately post a high-quality, thoughtful question to Stack Overflow.

Anyway, you probably use something like this already, but it’s just a reminder that the technique is available.

Angular.ts – Angular and TypeScript

Just a short one, but I’ve started writing an Angular app with TypeScript.

There is a fairly big cognitive difference between the two systems. This seems to be typical of anything that works through conventional naming, and Angular.js is more full of conventions and magic strings than anything I’ve seen for a while.

This meant I had a fairly serious bit of fiddling to do to get Angular working with my TypeScript controllers. Setting up modules isn’t a terribly natural for, at least to my brain. Luckily, this guy has already walked the same ground, and has figured out some decent patterns for structuring your app. Check him out if you want Angular magic with strong typing.

Introducing dotserver.windows; rapid graphviz development

An itch I wanted to scratch; when developing graphviz diagrams, it’s annoying to have to invoke the command-line compiler, then select the browser, then refresh;

Image

What I wanted was just this;

Image

So I wrote a console app which monitors a directory and compiles your `.dot` files whenever they change, producing a new `.gif` file. You can wrap it in a simple HTML page and use use LiveReload to automatically reload the gif version on every save. 

It’s on GitHub with an MIT license.

Taming dictionaries to strongly-typed interfaces with Castle DictionaryAdapter

So Castle is one of those projects that seems to sit behind a lot of technologies (Moq, NHibernate) but which maybe people don’t look at directly. Which is a shame, because it’s pretty special. Here’s one block of code with a really interesting effect;

// Let's define an interface...

public interface ICustomerProfile
{
    string UserName { get; set; }
    int NumberOfTimesSiteAccessed { get; set; }
}

// now let's have Castle give us a 
// strongly-typed wrapper onto a 
// simple dictionary object;
class Program
{
    static void Main(string[] args)
    {
        var dictionary = new Dictionary<string, object>();
        var factory = new Castle.Components.DictionaryAdapter
            .DictionaryAdapterFactory(); 
        ICustomerProfile proxy = factory.GetAdapter<ICustomerProfile>(dictionary);
        proxy.UserName = "John Appleseed";
        Console.WriteLine(proxy.UserName);
        Console.WriteLine(proxy.NumberOfTimesSiteAccessed);
    }
}

This uses Castle’s DictionaryAdapter to create a strongly-typed interface to a dictionary. Castle is implementing ICustomerProfile for us, and intercepting any calls, translating them into calls to a dictionary. Those two lines in red do all the interesting work.

What we’ve done is taken something that feels dynamically typed and ‘frameworky’ (the dictionary) and make it a proper, strongly-typed domain object again.

As the docs point out, this is really useful when dealing with ASP.NET, where there are all kinds of dictionaries, like ViewState, Session state, and HttpRequest.Form contents flying about.

You could do this by writing your own adapter class, get and set accessors for each property, but why bother?

T4MVC for removing magic strings from Razor views.

If you haven’t used T4MVC before, it’s a very nice NuGet package that helps remove some of the ‘magic strings’ from MVC views. Normally, you would create a link to a controller action in razor like this;

@Html.ActionLink("Link Text",
    "Development", "ChangeConnectionString", new { connectionString= "DataSource=foo" })

Which assumes there is a DevelopmentController with an action method like;

ActionResult ChangeConnectionString(string connectionString) 
{
 ...
}

T4MVC is a T4 (text templating) file which scans all your controllers and makes strongly-typed methods for specifying the controlller, actions, and parameters, which you use like this;

@Html.ActionLink("Link Text", 
    MVC.Development.ChangeConnectionString("DataSource=foo"))

Notice how we’ve removed the magic strings for the controller name and method name, and made the parameters more compact. If an action method name changes, or the parameter signature changes, T4MVC’s methods now stop compiling, and you get compile-time errors in your views, rather than runtime errors.

T4MVC needs to be run whenever a controller is added or an action method signature changes. This can be easy to forget, so I recommend the AutoT4MVC extension.

NCrunch; background running of unit tests in Visual Studio.

I’ve recently started trying NCrunch. It’s commercial software with a 60-day trial which helps you make sure you are getting good code coverage and you’re not breaking tests.

What it does is fairly straightforward. It’s a test runner, like VS’s Test Explorer, except that it automatically builds your tests and runs them, optionally on many threads, in the background. It then reports on the health of those tests, so if you start breaking tests, you know about it quickly.

It also shows you, in the VS document editor, which lines are covered by unit tests. So you can see where you need to write more tests.

There’s a nice writeup on Olav Nybø‘s blog.

It reminded me of using jasmine-node, which is also a really nice way to automatically run your tests and have them re-run when a file is saved, although that works for JavaScript tests.

 

 

Poor Man’s T-SQL Formatter Addin: Useful for devs!

A bit of love for the Poor Man’s T-SQL Formatter addin to MSSQL Management Studio. Allows you to reformat any query window in Management Studio to apply a consistent format. Really nice ‘starter for ten’, letting you clean up a batch of SQL after a hacking session. Why not look just that little bit more professional, for free?

insert into dbo.ItemBase ([Id],[Name] ,[Modified] ,[Created] ,[EntityStatus]) 
select [Id] ,[Name] ,[Modified] ,[Created] ,[EntityStatus]
from dbo.MyTable

to this;

INSERT INTO dbo.ItemBase (
[Id]
,[Name]
,[Modified]
,[Created]
,[EntityStatus]
)
SELECT [Id]
,[Name]
,[Modified]
,[Created]
,[EntityStatus]
FROM dbo.MyTable

NB: I’ve not tried the Visual Studio addin, just the MSSQL one. I just thought I’d keep things a bit cleaner. It might work brilliantly, but I’ve not tried it.

Pluralsight – a short review

Down at DevWeek 2013, I was given a card for one month on Pluralsight; they do video courses for all manner of programming and technology courses. 

I activated it this weekend, and drank from the firehose. I’ve gone through several courses in a really short time. In that time, I’ve also been able to cut some working code in the new tech, and shore up some existing knowledge. Here’s what I’ve figured out;

  • Sometimes, video is much faster to absorb than books. If you learn that way, give them a look.
  • The courses are comprehensive. By that I mean that I feel more ‘armed’ than than the ‘thirty minutes of tutorials and short how-to videos’ approach to learning. In that, they are like a book.
  • They seem to have courses for every buzzword; 3 on node.js, 5 on nosql, 2 on OData, 16 on Azure. And loads on the staples; 33 on SQL Server, 28 on JavaScript,  12 on iOS, 38 on ASP.NET. And 73 on SharePoint (shudder. It must need a lot of explaining…)

I’ve stuck in my credit card details because I think I’m going to benefit greatly. 

I know this is a terribly positive review. I’m not affiliated in any way. 🙂