Skip to content

Ability to Mock Multiple Dapper Calls at a Time #28

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

Open
steventmayer opened this issue Oct 15, 2018 · 11 comments
Open

Ability to Mock Multiple Dapper Calls at a Time #28

steventmayer opened this issue Oct 15, 2018 · 11 comments

Comments

@steventmayer
Copy link

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
Copy link
Owner

UnoSD commented Oct 28, 2018

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
Copy link
Author

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
Copy link
Owner

UnoSD commented Oct 29, 2018

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

@shahabganji
Copy link

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
Copy link

kaijday commented Apr 17, 2020

@steventmayer @UnoSD @shahabganji

Did this get resolved?

@IanKeefer
Copy link

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

Thanks

@hampton1122
Copy link

Is there a solution to this yet?

@IanKeefer
Copy link

IanKeefer commented Oct 9, 2020

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
Copy link
Owner

UnoSD commented Jan 29, 2022

        [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
Copy link

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
Copy link
Owner

UnoSD commented Jan 8, 2024

@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.

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

7 participants