UnoSD / Moq.Dapper

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

.ThrowsAsync doesn't appear to work #55

Open td0g-Z opened 4 years ago

td0g-Z commented 4 years ago

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

UnoSD commented 4 years ago

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.

td0g-Z commented 4 years ago

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

rickyricky74 commented 3 years ago

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

AliAdravi commented 1 year ago

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

ncipollina commented 1 year ago

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

LucasLopesr commented 10 months ago

anything news?

Pramod2392 commented 8 months ago

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

//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