Skip to content
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

.ThrowsAsync doesn't appear to work #55

Open
td0g-Z opened this issue Feb 11, 2020 · 7 comments
Open

.ThrowsAsync doesn't appear to work #55

td0g-Z opened this issue Feb 11, 2020 · 7 comments

Comments

@td0g-Z
Copy link

td0g-Z commented Feb 11, 2020

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

UnoSD commented Feb 12, 2020

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

td0g-Z commented Feb 13, 2020

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

@rickyricky74
Copy link

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

AliAdravi commented Feb 12, 2023

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

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

@LucasLopesr
Copy link

anything news?

@Pramod2392
Copy link

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

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