SQL Server Spatial Types and nUnit

I was recently working on a .NET 4.6 based project that was using EF 6 and nUnit for unit testing. While setting up some integration tests against a local SQL database I was receiving this error:

Spatial types and functions are not available for this provider because the assembly ‘Microsoft.SqlServer.Types’ version 10 or higher could not be found.

We had recently been using SQL Server spatial types for tracking geograpic locations and the tests which performed updates and inserts against these fields were failing.

The microsoft.sqlserver.types package installed in our application and we had followed the readme.htm that is included with that package. Our actual application was working fine following this setup, but the tests were still failing.

The readme.htm addresses how to load the spatial types from different application types (asp.net, windows forms,), but it didn’t address how to load it for nunit tests. It took a little research and some experimenting, but I finally got it to work. Here are the steps:

  1. Install the microsoft.sqlserver.types package into the integration test project:
Install-Package Microsoft.SqlServer.Types -Version 14.0.314.76
  1. Add a test fixture class
using System.Data.Entity.SqlServer;
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;

namespace TopLevel
{
[SetUpFixture]
public class SetUpFixture
{
[OneTimeSetUp]
public void SetUp()
{
SqlServerTypes.Utilities.LoadNativeAssemblies(GetAssemblyDirectory());

SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";
}

public string GetAssemblyDirectory()
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);

return Path.GetDirectoryName(path);
}
}
}

The nUnit SetUpFixture class runs once before all of the fixtures for the given namespace. Since this is running at the toplevel namespace, it will run the OneTimeSetUp method once before starting to run all of the other tests in the assembly.

The SetUp method loads the native assemblies from the bin folder of the currently running test assembly. Since the package is installed in the test project, the build will copy the SQLServerTypes folder (added by nuget) and the two contained assemblies out to the bin folder.

The GetAssemblyDirectory method simply returns the current bin directory path being used to run the execute the tests. This is provided to the Loader.cs utility included in the SqlServer types nuget package so that it can load the assemblies into the process.

The second line of the OneTimeSetup calls into the Entity Framework SqlProviderServices to override the full assembly name that EF is using to reference the types assembly. This ensures Entity Framework references the same version being loaded in the test project.

Hopefully this explations helps and keeps others from struggling with setting up EF6 integration tests using nUnit and spatial types!

One thought on “SQL Server Spatial Types and nUnit

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.