Moq.Dapper icon indicating copy to clipboard operation
Moq.Dapper copied to clipboard

Cannot moq 'Execute' after mocking 'QuerySingle<int>'

Open Abinaya-Krishnan opened this issue 4 years ago • 0 comments

I want to mock both QuerySingle and Execute functions. Mocking 'Execute' after QuerySingle throws error. The error says

Message:

  • System.NullReferenceException : Object reference not set to an instance of an object.

Stack Trace:

  • SqlMapper.QueryRowImpl[T](IDbConnection cnn, Row row, CommandDefinition& command, Type effectiveType) line 1180

  • SqlMapper.QuerySingle[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable1 commandType) line 785

  • SqlMapper.QuerySingle(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable1 commandType) line 691

Please see the following example: var moqConnection = new Mock<IDbConnection>(); const int expected = -44;

moqConnection.SetupDapper(c => c.QuerySingle("yyy", null, null, null, null)).Returns(-4); moqConnection.SetupDapper(c => c.Execute("zzz", null, null, null, null)).Returns(-44);

var actual1 = moqConnection.Object.QuerySingle("yyy"); var actual2 = moqConnection.Object.Execute("zzz");

In the above example, if I swap 'QuerySingle' and 'Execute' then the code works.

Abinaya-Krishnan avatar Nov 02 '21 23:11 Abinaya-Krishnan