Testing Testing - where is my database?

Publish date: 10 Jun 2016
Tags: software

Recently while creating a unit test (MSTest VS2015) to test my repository pattern, I hit a snag. Despite having defined the database in my app.config of the Test project, I could not locate my newly created database.

Turns out I needed to initialise my db in the test class , like so:

<pre class="wp-code-highlight prettyprint linenums:1">// Declare this property - this is set by MSTest
 public TestContext TestContext { get; set; }

 // In test initialization 
 public static void SetUp(TestContext context)
 AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(context.TestDeploymentDir, string.Empty));

Saw this useful tip at Stackoverflow, which also talks about the differences between sdf (Server Explorer does not show table schema) and mdf (Server Explorer displays table schema).

Also came across this interesting article on locating your LocalDB.