DapperLib / DapperLib/Dapper

Issue mocking(moq) ExecuteAsync for unit test(xunit)

Open
#1,667 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

a moq of ExecuteAsync in my unit testing is throwingInnerException = {"Async operations require use of a DbConnection or an IDbConnection where .CreateCommand() returns a DbCommand"}. Execute seems to work fine.

private  void SetupExecuteAsync ()
        {
            var dataParameterCollection = new Mock<IDataParameterCollection>();
            dbCommandMock.Setup(x => x.Parameters).Returns(dataParameterCollection.Object);
            dbCommandMock.Setup(x => x.ExecuteNonQuery()).Returns(It.IsAny<int>());
            dbConnectionMock.Setup(s => s.CreateCommand()).Returns(dbCommandMock.Object);
        }

I am mocking DbCommand object and Execute and ExecuteAsync seem to have the same parameters so I'm not sure why I am getting this error.

return connection.Execute(sql, dbParams, commandType: commandType);
 vs
return await connection.ExecuteAsync(sql, dbParams, commandType: commandType).ConfigureAwait(false);

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin at Dapper's ExecuteAsync entry point and compare it with Execute, then trace the connection's CreateCommand path and the DbCommand setup shown in SetupExecuteAsync. Reproduce the xunit/Moq failure and verify that the async call completes without the reported DbConnection/DbCommand exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
database, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.