4

I have below method :

public List<ITestKeyword> AddTests(TestEntity testEntity)
{
    var DesignSteps = new List<ITestKeyword>();
    foreach (var testCase in testEntity.TestCases)
    {
        DesignSteps.AddRange(testCase.GetTestStepKeywords());
    }
    return DesignSteps;
}

It is invoked as below:

var listCount= _TestHelper.AddTests(testEntity).Count;

Here is how I try to mock it :

_mockTestHelper
    .Setup(s => s.AddTests(It.IsAny<TestEntity>()))
    .Returns(It.IsAny<List<ITestKeyword>>());

But it doesn't seem to work. It is throwing null reference exception. I'm not able to figure out. Could anyone please help?

Nkosi
  • 191,971
  • 29
  • 311
  • 378
SKN
  • 488
  • 1
  • 4
  • 17

1 Answers1

3

Try this :

var testList = new List<ITestKeyword>();

_mockTestHelper
    .Setup(s => s.AddTests(It.IsAny<TestEntity>()))
    .Returns(testList);

That way you can populate your list as you like

Nkosi
  • 191,971
  • 29
  • 311
  • 378
Alex
  • 2,974
  • 3
  • 30
  • 70
  • 1
    Great. It works.. But i don't understand how. Do you mind elaborating it a bit? – SKN Feb 22 '17 at 11:33
  • You are trying to return It.IsAny>() which is basically saying return any instance of this item. But how can it return Any random instance of something. The answer creates an actual object that will be returned each time this funciton is called within your testing framework. I use It.IsAny when mocking the inputs of a method not the output eg to tell a method that whenever any int is passed in, return the specified value. – Alex Feb 22 '17 at 11:47
  • 1
    @Alex Well explained. I got it. Appreciate your time. – SKN Feb 22 '17 at 12:04