Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Abinaya-Krishnan opened this issue Nov 2, 2021 · 0 comments
Open

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

Abinaya-Krishnan opened this issue Nov 2, 2021 · 0 comments

Comments

@Abinaya-Krishnan
Copy link

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();
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant