Rahul
Rahul

Reputation: 77846

Unit Testing using VS2010

I am unit testing a method say ABC() like

myclass target = new myclass();
target.ABC();

This ABC() method in turn calls another method XYZ() from a different class like anotherclass.XYZ() and this XYZ() method's input parameter depends on values from textbox (s).

Since, at test run there is no values from textbox passed I am getting a null referrence exception while running the test (I mean test fails with exception).

Similar situation (another): method fetches id value from querystring like

HttpContext context
id = context.request.querystring["id"]

Since, at test run this id values is null I get a null referrence exception(I mean test fails with exception).

To my understanding, logically this is correct since this is test run and not actual run but still want to confirm once ...

Am I doing anything wrong?

OR

My test is functioning correct?

Please suggest. Appreciate your help.

Thanks.

Upvotes: 1

Views: 318

Answers (2)

Russell Troywest
Russell Troywest

Reputation: 8776

I have no idea what your code looks like but it probably needs to be refactored a little bit to allow unit testing.

For instance, if you have code like this:

MyClass{

  private AnotherClass _another = new AnotherClass();

  public string ABC(){
    return _another.XYZ();
  }
}

public AnotherClass{
  //Context comes from somewhere...
  public string XYZ(){
    return context.request.querystring["id"]
  }
}

You would want to change it to something like this:

interface IAnotherClass{
  string XYZ();
}

MyClass{  
  private IAnotherClass _another = new AnotherClass();

  public MyClass(IAnotherClass anotherClass){
    _another = anotherClass;
  }

  public string ABC(){
    return _another.XYZ();
  }
}

public AnotherClass: IAnotherClass{
  //Context comes from somewhere...
  public string XYZ(){
    return context.request.querystring["id"]
  }
}

You can then use a mocking framework to mock up the IAnotherClass as IceN has show. As for the HttpContext - that becomes a little more complicated, but if it's only used by the AnotherClass you wont need to mock it since the mock object will just return whatever you like. Obviously when you come to test AnotehrClass it becomes an issue. If you find you do need to mock it then there are a number of examples of what to do - for instance this one.

Upvotes: 1

Nastya Kholodova
Nastya Kholodova

Reputation: 1321

You need to use "not real" instance of anotherclass, but Mocked instance with fake XYZ implementation.

I'll make an example with Moq framework.

Mock<IAnotherclass> anotherClassMock= new Mock<IAnotherclass>();//create mock instanse
anotherClassMock.Setup(x=>x.XYZ);//add .Returns(something) if method XYZ returns some value
myclass target = new myclass(anotherClassMock.Object);//create instance of object under test with fake instance of anotherClassMock
target.ABC();
anotherClassMock.Verify(x=>x.XYZ);//verify that method XYZ is actualy been called

First of all you need to extract an interface of class anotherclass in order to create Mock. In my example I used an interface IAnotherclass.

Why we are doing like that? We do not intresded in logic of class anotherclass. We assume it is correct. So we abstracting from it and testing only logic of myclass. And leter we only veryfy that method XYZ is has been called.

Upvotes: 1

Related Questions