Materials
- Visual studio 2008
- SQL Server 2005 Express edition
- Visual Studio Unit Testing Framework
Ingredients
- System.Linq - In order to support LINQ queries in our code
- System.Data.Linq - In order to support the Linq2SQL datacontext etc
- Linq2SQL datacontext
I was creating this application which heavily depended upon a database.
In order for me to test the business rules in this application, I needed to abstract out the database so it wouldn't slow down my tests or corrupt them or change/delete any of the data in the database after each test.
After scouering the internet I found a solution here:
http://andrewtokeley.net/archive/2008/07/06/mocking-linq-to-sql-datacontext.aspx
It basicly creates a linq to sql database in-memory. That's right, in-memory!
I noticed some people have a hard time implement it though, hence this article.
I'm sharing the source for you all to enjoy.
Detailed description
I created a solution in which we have 3 projects.- A unit testing project
- A website project
- A business project
In the website project you'd do most of the presentation layer work.
The business project would be used to hold the business rule classes and the Linq2Sql datacontext classes.
In the unit testing framework we'd include testclasses to test our business rules.
The important thing in the solution is this:
We use the same exact function to show the users in the website as we do for testing in our unit test. In the unit test users are queried from the in-memory database, in the website we use our real SQL Server database. This is done by one configuration appsetting value in the web.config of both projects.
<appSettings>
<add key="UseTestDatabase" value="false"/>
</appSettings>
Prepare the database
I want to make sure you need to do as little as possible to get up and running with this example. In the example the datacontext has already been configured to a database, and to make sure you don't have to juggle with connectionstrings I want to create a database alias.After we created this alias we are going to create a database and some membership tables with help of aspnet_regsql.exe. I use this database and alias in all my examples so it's better that you do this.
create alias
Go to all programs -> Microsoft SQL Server 2005 -> Configuration Tools -> Sql Server Configuration Manager

create database

create membership tables
Run aspnet_regsql.exe at location:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
In the wizard select your newly created database named CodeCookBook

Run the example
Open the solution in visual studio.Run the tests as shown in the screenshot.

In the test class we first create the tables in memory, then populate it with two aspnet_User entities and then check if we have more than 1 user. It ofcourse will. It just illustrates how you can test with this in-memory database.
Now make sure the website is the startup project. Run the example (Ctrl+F5).
Create 2 users. And go to show users to reassure yourself they are stored in the database. These users are stored in the real database.
so the tests run with the in-memory database, and the website with the real database all because of one config value <add key="UseTestDatabase" value="false"/>
download source code
No comments:
Post a Comment