Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
Im testing out some testing software well NUnit and TDD. while I can get TDD to work with no problem I am struggling to get NUnit working property.

Below is a simplified version of the class that I am running the test on.
C#
public class buildings : RatingBase
{
  public override decimal Premium()
  {
    //have stripped this down to show only the area that is causing the error
    decimal TAMultipler = TA();
  }

  private decimal TA()
  {
    string regions = RegionResults(Postcode);
  }
}

public abstract class RatingBase
{
  public string RegionResults(string Postcode, string Town = "")
  {
    using(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["connectionstring"].ToString())
    {
    }
  }
}


Now under Microsoft TDD it works as expected fails / passes on the assertion depending on the values I put in to compare against the result of Premium()

But when I try NUnit with the following test, which is the same test used in the TDD attempt. I have tried it with both the mock of the datacontext and without and the result is still the same.

C#
[TestFixture]
public class TestBuildings
{
    [Test]
    public void CalculateBuidings()
    {
        //string connection = "Data Source=oak_apps;Initial Catalog=Oak_Underwriting;Integrated Security=True";
        //Mock<DataContext> db = new Mock<DataContext>(connection);

        Buildings blds = new Buildings();
        blds.Postcode = "TN20 6JS";
        blds.Age = 54;
        blds.Occupation = "Insurance Company Director";
        blds.PropertyType = "Detached";
        blds.YearBuilt = 2012;
        blds.SumInsured = 2878500;
        blds.Excess = 500;

        blds.PercentageAdjustment = 0.855M;
        blds.MoneyAdjustment = 0;
        blds.Discretionary = 10;

        decimal value = blds.Premium();

        Assert.AreEqual(1792.809M, value);
    }
}


When I run it though Nunit test application it always throughs the following error

Rating.TestBuildings.CalculateBuidings:
System.NullReferenceException : Object reference not set to an instance of an object.

Am I missing something that I need to setup in the NUnit test? any pointers to get it working would be great!

Thanks
Simon
Posted
Updated 2-Oct-13 21:28pm
v2
Comments
Rob Philpott 2-Oct-13 16:49pm    
I can't see anything intrinsically wrong with that but I think you'd need to include more code. In particular when you debug the test, what line throws the exception?
Simon_Whale 3-Oct-13 3:29am    
Thanks Rob, I've traced through the test and the line that caused the error is the using statement that connects to the database

What does the app.config that's used when running NUnit look like?

This sounds very much like a case of missing configuration, try making sure the configuration you need exists in the app.config of the test project.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Simon_Whale 3-Oct-13 5:37am    
Thank you very much Fredrik.

I assumed that the NUnit took the app config from the bin directory. After creating a app.config in the root of the rating class library project and running it again it worked as expected.
Fredrik Bornander 3-Oct-13 5:44am    
Glad I could help.
You should mark the answer as accepted if it solved your problem so that we can close the question.
Ok, based on that - two things.

Firstly, I suspect that you may be using NUnit test runner, so I think it will be looking in that thing's app.config for the connection string rather than you app. That'd explain why it works with Microsoft's approach.

Other thing (maybe not important):

using(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["connectionstring"].ToString())


Should be:

using(DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString)


Either way, I bet that you are calling ToString() on a null!
 
Share this answer
 
Comments
Simon_Whale 3-Oct-13 5:44am    
Rob Thank you for your time.

It was that I was running it through the test runner with out its own app.config, created the librarys own app.config and it works.
Rob Philpott 3-Oct-13 10:24am    
Cool! You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900