1

I've got a SqlLayer class I'm trying to Mock that has a GetDataReader method that accepts a string query and an IEnumerable of SqlParameters. I've tried stubbing out the method call like this:

var parameters = new SqlParameter[] {}
mockSqlLayer.Stub(x => x.GetDataReader(spaceConsumedQuery, parameters)).Return(MockDataReader());

But the test is returning a NullReferenceException, I assume because the signature isn't resolving correctly, and can't find the stub. The method signature I'm trying to call looks like this:

public IDataReader GetDataReader(string commandText, IEnumerable<SqlParameter> parameters)
will
  • 705
  • 2
  • 9
  • 20

1 Answers1

1

The argument matcher would need to be loosened to allow for the invocation to behave as expected.

mockSqlLayer
    .Stub(_ => _.GetDataReader(spaceConsumedQuery, Arg<IEnumerable<SqlParameter>>.Is.Anything))
    .Return(MockDataReader());

The code used originally would need to use the same object reference for the invocation to match.

The assumption here is that the value in variable spaceConsumedQuery is the same one used when exercising the subject under test.

If not, then Arg<string>.Is.Anything could be used to loosen that match as well.

Nkosi
  • 191,971
  • 29
  • 311
  • 378