0

I have a C# code as follows

public partial class ChildClass : ParentClass
{   
...
...
    public ChildClass() : base(SomeCore.Context.Item.Fields["Parameters Template Name"].ConvertToString())
    {
    this.memVarA = //reading from some Context
        this.memVarA = //reading from some Context    
    }
}

I want to create a test cases for ChildClass. In the test class I have this code:

[TestMethod()]
public void ItemsListShouldHaveOnly4Items()
{
    ChildClass ChildClass = new ChildClass();
    PrivateObject privateObject = new PrivateObject(ChildClass);
    privateObject.Invoke("aPrivateMethod");
    var retVal = privateObject.Invoke("getItemsList");
    Assert.AreEqual(4, retVal);
}

The test breaks with exception : System.NullReferenceException: Object reference not set to an instance of an Object.

How can I go about writing test cases for this class ?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
user1860447
  • 1,128
  • 5
  • 18
  • 41
  • What line throws the exception? What is `partial` here? – Ofiris Jan 22 '15 at 19:06
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 22 '15 at 19:06
  • Which version of Visual Studio are you using? Which unit test platform? MSTEST? NUnit? Also, you should always post the complete exception, including the stack trace. Post the results of ex.ToString() if at all possible. – John Saunders Jan 22 '15 at 19:07
  • 1
    And are you actually doing TDD like your tag says? Because if you were, there would be no code because you have no test that fails. – John Saunders Jan 22 '15 at 19:09
  • Also you generally don't see reflection in unit tests as one *should not* be unit testing implementation details, but rather behavior. – Alexei Levenkov Jan 22 '15 at 19:10
  • @John Saunders - I am usinf VS 2012. I am using - Microsoft.VisualStudio.TestTools.UnitTesting. That was all that was there in the exception. Very new to C# coding. The exception is at constructor line: public ChildClass() : base(SomeCore.Context.I ... – user1860447 Jan 22 '15 at 19:13
  • Trying to do TDD, but been so new to VS environment, haven't been able to create tests first. This is an existing code base with no test at all. I am starting to write tests for the code I am touching. – user1860447 Jan 22 '15 at 19:15
  • Either `SomeCore` or `SomeCore.Context` or `SomeCore.Context.Item` or `SomeCore.Context.Item.Fields` or `SomeCore.Context.Item.Fields["Parameters Template Name"]` is `null`. – John Saunders Jan 22 '15 at 22:58

0 Answers0