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

.ThrowsAsync doesn't appear to work

Open td0g-Z opened this issue 5 years ago • 7 comments

I am trying to simulate a query throwing an exception. So I can test how my code handles this.

I am doing a setup along the lines of

dbConnectionMock.SetupDapperAsync(mock =>
                        mock.QueryAsync<RowModel>(
                            It.IsAny<string>(),
                            It.IsAny<object>(),
                            null,
                            null,
                            null))
                    .ThrowsAsync(new Exception("error executing db query"));

The code errors whilst performing this setup and throws an exception with the following message: One or more errors occurred. (error executing db query)

It appears almost as if my dummy exception is being thrown during the setup.

Am I doing something wrong? Or is .ThrowsAsync not supported?

Thanks for making this library it is very useful Tom

td0g-Z avatar Feb 11 '20 17:02 td0g-Z

Hi Tom, thank you for spotting this; unfortunately I have moved to a different role and I am not using this library so it is not under active development.

Throw/ThrowAsync is not currently implemented, but I would be more than happy to review a PR if anyone updated the code to support it.

UnoSD avatar Feb 12 '20 15:02 UnoSD

Hi, thanks for the reply. If I get some free time I'll try to have a look at it 👍

td0g-Z avatar Feb 13 '20 11:02 td0g-Z

It would be nice if support for this was implemented. Our code has a gap in code coverage because of this. Thanks in advance.

rickyricky74 avatar Feb 18 '21 17:02 rickyricky74

You can cover you code by throwing synchronously Here is the code working for me: On an async method 1- ReturnsAsync 2- Throw

var stub = new Stub();
var expected = stub.dummyRecords;
 stub.dbConnection.SetupDapperAsync(x => x.QueryAsync<MyModel>(It.IsAny<string>(), null, null, null, CommandType.StoredProcedure))
    .ReturnsAsync(expected);
 var result = await stub.repository.GetRecordsById(Guid.NewGuid());
 Assert.NotNull(result);
// Catch block 
 stub.dbConnection.SetupDapperAsync(x => x.QueryAsync<MyModel>(It.IsAny<string>(), null, null, null, CommandType.StoredProcedure))
               .Throws(new Exception());
 var act = async () => await  stub.repository.GetRecordsById(Guid.NewGuid());
_ = Assert.ThrowsAsync<Exception>(act);

Hope it will help

AliAdravi avatar Feb 12 '23 02:02 AliAdravi

@AliAdravi this is a false positive. When doing this, your code isn't actually throwing the exception.

ncipollina avatar May 08 '23 18:05 ncipollina

anything news?

LucasLopesr avatar Dec 20 '23 18:12 LucasLopesr

Even I am facing similar problem, the SetupDapperAsync code to throw exception doesn't actually throw exception

//mock moqSQLDBConnection.SetupDapperAsync(x => x.QueryAsync(It.IsAny(), addBookModel, null, null, CommandType.StoredProcedure)).Throws<Exception>();

//code var queryResult = await _connection.QueryAsync("dbo.spo.AddBook @name, @purchasedDate, @price, @imageBlobURL, @categoryId", new { name = model.Name, purchasedDate = model.PurchasedDate, price = model.Price, imageBlobURL = model.ImageBlobURL, categoryId = model.CategoryId });

I am trying to mock _connection.QueryAsync to throw exception, but it doesn't throw exception

Pramod2392 avatar Feb 11 '24 11:02 Pramod2392