RSS

Tag Archives: test driven development (tdd)

Good Unit Test Qualities

  • Repeatable
    Unit tests should be repeatable. If a test fails, it should always fail, if it succeed, it should always be.
    Good unit tests also repeatable all the time, not succeed at certain time, but fail at other times.
  • Independent
    Each unit test should be independent, meaning it should not depend on other unit test for it to run.
    You should also be able to run each unit test by itself, or a select group of tests.
Advertisements
 
Leave a comment

Posted by on November 6, 2014 in General

 

Tags: ,

HttpContext vs HttpContextBase vs HttpContextWrapper

HttpContext

Good ol’ HttpContext in ASP.Net. HttpContext has no base class and isn’t virtual, thus, is unusable for testing and unmockable. Strongly recommended to not pass it around as arguments. Instead, pass around variables of type HttpContextBase.

HttpContextBase

New to C# 3.5 and meant to replace HttpContext. HttpContextBase is abstract, thus, it is now mockable. Concretely implemented by HttpContextWrapper.

HttpContextWrapper

Also new in C# 3.5 – this is the concrete implementation of HttpContextBase. To instantiate in a normal webpage:

var webContext = new HttpContextWrapper(HttpContext.Current)

The whole idea is to make your code test-able by allowing your test framework to mock the HttpContext.

More detail explanation on here.

 
Leave a comment

Posted by on April 18, 2013 in General

 

Tags: , , ,

The Last (Hopefully) NUnit Error: The Type Initializer for … Threw an Exception (And app.config)

After series of NUnit Error: The Type Initializer for … Threw an Exception: First One and Same One, I concluded (and hopefully this is the last post for the topic):

  1. If you open NUnit project with NUnit, the app.config file will have to be in the same folder as the NUnit project file and have the same name as the project file name:
    C:\Users\Documents\Nunit\ProjectTest.nunit
    C:\Users\Documents\Nunit\ProjectTest.config
  2. If you open Visual Studio .dll file, the app.config will have to be in the same folder as the project file and have the same name as the project file name (including the extension):
    C:\Users\Documents\Nunit\ProjectTest.dll
    C:\Users\Documents\Nunit\ProjectTest.dll.config
  3. If you open Visual Studio project (.csproj) or solution (.sln) file with your NUnit, the app.config will have to be in the same folder as the project file and have the same name as the project file name:
    C:\Users\Documents\Nunit\ProjectTest.csproj
    C:\Users\Documents\Nunit\ProjectTest.config
 
Leave a comment

Posted by on July 27, 2011 in General

 

Tags: , , , , ,

Same Error in NUnit: The Type Initializer for … Threw an Exception (And app.config)

I have already copied app.config file as instructed here but still get the same error. So, what could be the problem here?

Apparently, this error simply means the system not able to access database, namely: Connection String problem.

This is how I access my connection string:

System.Configuration.ConfigurationSettings.AppSettings.Get("DatabaseEntities");

Above code will not return the connection string I have in web.config file. I use Entity Framework and in Entity Framework, the correct way to get connection string is:

System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseEntities"].ConnectionString;
 
2 Comments

Posted by on July 26, 2011 in General

 

Tags: , , , , ,

NUnit Error: The Type Initializer for … Threw an Exception (And app.config)

My Visual Studio solution contains 5 projects, one of them is for unit testing purpose.

The unit testing project is referencing to Entity Framework library where I query data from database. Well, since the Entity Framework is depending on app.config for the connection string, my NUnit returns error when I run it. This is because NUnit can’t find the connection string in app.config file. The error is:

System.TypeInitializationException: The type initializer for ‘…’ threw an exception.
System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Here’s the screenshot (some information is blacked out on purpose). I use NUnit 2.5.9 for unit testing.

Two workarounds for this problem are stated below. You don’t have to apply both workarounds, just either one of them. In my case, the first one works like charm.

  1. Copy app.config file, paste it in the same folder as your .nunit file and rename it to the same as your NUnit project filename.
    Example: NUnit project file is “C:\Users\Documents\Nunit\ProjectTest.nunit”. My app.config will have to be “C:\Users\Documents\Nunit\ProjectTest.config”
  2. Copy app.config file, paste it in the same folder as your library (.dll) file and rename it to the same as your library (.dll) filename.
    Example: Library project file is “C:\Users\Documents\ProjectSolution\ProjectTest\bin\Debug\ProjectTest.dll”. My app.config will have to be “C:\Users\Documents\ProjectSolution\ProjectTest\bin\Debug\ProjectTest.dll.config” (if doesn’t work, try rename it to “C:\Users\Documents\ProjectSolution\ProjectTest\bin\Debug\ProjectTest.config”)
 
3 Comments

Posted by on March 2, 2011 in General

 

Tags: , , , ,

 
%d bloggers like this: