UnoSD / Moq.Dapper

Moq extensions for Dapper methods.
GNU General Public License v2.0
173 stars 78 forks source link

Ability to Mock Multiple Dapper Calls at a Time #28

Open steventmayer opened 6 years ago

steventmayer commented 6 years ago

I am attempting to test a feature that relies on multiple QueryAsync; however, when apply the second setup, it overrides the first mock setup, even if T is different. I was hoping that it would work differently for different T's or by changing the SQL statement, but no luck. Code looks like this.

var mock = new Mock(); mock.SetupDapperAsync(c => c.QueryAsync(It.IsAny(), null, null, null, null)).ReturnsAsync(object1); mock.SetupDapperAsync(c => c.QueryAsync(It.IsAny(), null, null, null, null)).ReturnsAsync(object2);

var expected = object1; var actual = mock.Object.Query("", null, null, null, null).ToList(); // shows up as Object2.

I'll ask on SO to see if I'm missing something. Thanks for this great library.

UnoSD commented 6 years ago

Hi @steventmayer, I may have some time in December to look at this, from a first glance at the specs, I would check the behaviour of Moq in this case, I have a feeling that it overrides the previous setup, too. One way to get around this would be to add a SetupSequence which I think may be not so difficult to implement and you would be able to setup multiple objects to return.

steventmayer commented 6 years ago

Let me try playing around with SetupSequence . One area I was looking at was the ability to limit it if I declared the SQL ( c.QueryAsync("SP.StoredProcName" above instead) but need to look further into expressions. If I'm able to get something working, I'll put a PR in and see if you're interested.

UnoSD commented 6 years ago

Thanks, I'll do my best to find some time to look at the PR as soon as possible.

shahabganji commented 5 years ago

Let me try playing around with SetupSequence . One area I was looking at was the ability to limit it if I declared the SQL ( c.QueryAsync("SP.StoredProcName" above instead) but need to look further into expressions. If I'm able to get something working, I'll put a PR in and see if you're interested.

@steventmayer

Do you mind if I ask for a simple sample?

kaijday commented 4 years ago

@steventmayer @UnoSD @shahabganji

Did this get resolved?

IanKeefer commented 4 years ago

@steventmayer @UnoSD would like an update on the status of this if possible.

Thanks

hampton1122 commented 4 years ago

Is there a solution to this yet?

IanKeefer commented 4 years ago

Is there a solution to this yet?

I haven't ran into one myself and our unfortunate result so far is omitting the functions from code coverage.

Looks as if this repo is finished as the owner hasn't been active here or on GitHub in a long time.

UnoSD commented 2 years ago
        [Test]
        public void Callback()
        {
            var connection = new Mock<IDbConnection>();

            int[] firstExpected = { 15 };
            int[] secondExpected = { 20 };

            IEnumerable<int> expected = firstExpected;

            connection.SetupDapper(x => x.Query<int>(It.IsAny<string>(), null, null, true, null, null))
                      .Returns(() => expected)
                      .Callback(() => expected = secondExpected);

            var firstActual = connection.Object.Query<int>("");
            Assert.That(firstActual, Is.EquivalentTo(firstExpected));

            var secondActual = connection.Object.Query<int>("");
            Assert.That(secondActual, Is.EquivalentTo(secondExpected));
        }

this may work as a solution. getting it to work appears to be way more complicated than I thought; it's hard to get the previous return value from Moq even with reflection and the ugly alternative is to keep a dictionary of the mocks and return values. I'm going for the hack, but new problems are bubbling up

yogeshk97 commented 9 months ago

Is there any update on this issue please? This hack is not working for me @UnoSD

        _mockIDbConnection
           .SetupDapperAsync(c => c.QueryAsync<ComboDishListModel>(
               It.IsAny<string>(), // You can specify the exact SQL query if needed
               It.IsAny<object>(),  // You can specify the expected parameters if needed
               It.IsAny<IDbTransaction>(),
               It.IsAny<int?>(),
               It.IsAny<CommandType?>()))
           .ReturnsAsync(comboDishListModels); // Provide the expected result

        _mockIDbConnection
.SetupDapperAsync(c => c.QueryAsync<OrderViewModel>(
    It.IsAny<string>(),
    new { command.OrderId, command.UserId },
    It.IsAny<IDbTransaction>(),
    It.IsAny<int?>(),
    It.IsAny<CommandType?>()))
.ReturnsAsync(ordersResult);

When apply the second setup, it overrides the first mock setup, even if T is different

UnoSD commented 9 months ago

@yogeshk97 at the moment I do not have the capacity for working on the repo, but I welcome PRs and I am happy to review.