I have a class Class1
, which has a constructor and few methods. For these methods I'm writing the unit test cases using MSTest. The class looks like this.
class Class1
{
Order order = new Order(); // there is an class Order
public Class1(Order _order)
{
order = _order;
}
public virtual async Task<bool> GetData()
{
if(order != null)
{
//do something
}
else
{
// do something else
}
}
}
Now, I have to write 2 test cases for this GetData() method, when which tests if block and one which tests the else block. I was able to test the if block successfully, but not able to test the else block. The test method which I'm trying to write is as below.
[TestMethod()]
public void GetDataTest()
{
Order order = new Order();
order = null;
var mockService = new Mock<Class1>(order)
{
CallBase = true
};
var result = await mockService.Object.GetData(); // error thrown from here
Assert.IsFalse(result);
}
What I'm trying to do is set the order object to null and pass the null object to the constructor.But this is throwing some error "Ambiguous match found". Clearly passing the null value is not working here. So can anyone tell me any other work around to test the else block.
PS: I need to test both if and else block so that it is included in the code coverage.