Wednesday, May 12, 2010

Testing Subsonic based projects

Testing Subsonic based projects is a breeze. No mock frameworks needed. All you have to do is add an app.config file and some mock objects righ into Subsonic. Have a look at the following:

In the App.config file of my test project:

<connectionstrings>
<add name="VisenoConnection" connectionstring="Test">
</add>
</connectionstrings>

My Setup method:
[SetUp]
public void StartTest()
{
List companies = new List();
for (int i = 0; i < 100; i++)
{
VisenoCompany company = new VisenoCompany();
company.Id = i;
company.Name = "webtop";
company.Address = "address";
companies.Add(company);
}

companies[1].CompanyKey = "123";

VisenoCompany.Setup(companies);
}

My test method:
[Test]
public void TestGetCompany()
{
CompanyRepository companyRepository = new CompanyRepository();
VisenoCompany company = companyRepository.Get(1);
Assert.That(company.Id == 1);
Assert.That(company.CompanyKey == "123");
Assert.That(company.Name == "webtop");
(...)

Subsonic now uses memory in stead of database. In this case the repositories and generated classes are even in a different project.