1

I've slimmed down some code I'm trying to test to the example below

I've followed the advice I found online and carried out the setup below.

My problem is when the test runs it never returns from the Task.WhenAll

I'm confined to .net 4.5.2 so don't have Task.FromException at my disposal.

The code that calls the dependency is:

    public async Task<int> CountCustomFields(bool onlyVisible)
    {
        var countClientCustomField = clientService.CountCustomFieldsAsync(onlyVisible);
        try
        {
            await Task.WhenAll(countClientCustomField);
        }
        catch (Exception ex)
        {
            // some loggging etc
        }
        finally
        {
            // some tidy up & set the result
        }
        return result;
    }

Mocking is configured in the following test method:

    [Test]
    public void WhenAsyncMethodThrows_WeShouldCatchTheException()
    {
        mockClientService.Setup(x => x.CountCustomFieldsAsync(It.IsAny<bool>()))
            .Returns(new Task<int>(() => { throw new Exception("err"); }));

        Assert.DoesNotThrowAsync(() => sut.CountCustomFields(true));
    }

I'm updating the example to include the dependency mentioned in the comments. I think it can be ignored that this is a microservice. In fact it could be any dependency which returns a Task

public interface IClientService
{
    Task<int> CountCustomFieldsAsync(bool onlyVisible);
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
user48408
  • 3,234
  • 11
  • 39
  • 59
  • What is `clientsClient`? – Ant P Jan 31 '17 at 11:52
  • Its a webapi microservice – user48408 Jan 31 '17 at 11:56
  • That doesn't help us debug it - you have a Task that is apparently hanging but you haven't included any of the code relevant to the class/method that returns that task. – Ant P Jan 31 '17 at 11:58
  • @AntP `CountCustomFieldsAsync` is the action getting *mocked* it's implementation is irrelevant – Panagiotis Kanavos Jan 31 '17 at 12:04
  • I'm not sure its correct to say its hanging. But it is not returning and throwing an exception like I believe my setup has instructed it to – user48408 Jan 31 '17 at 12:05
  • yes, i haven't made that clear as i haven't shown it being created but mockService is a new Mock – user48408 Jan 31 '17 at 12:06
  • @PanagiotisKanavos really? I don't see it getting mocked anywhere. – Ant P Jan 31 '17 at 12:08
  • @AntP `mockService.Setup(x => x.CountCustomFieldsAsync(It.IsAny()))` ??? – Panagiotis Kanavos Jan 31 '17 at 12:09
  • Ok. So that's mockService. What about clientService? – Ant P Jan 31 '17 at 12:10
  • I've updated. It could have been clearer – user48408 Jan 31 '17 at 12:11
  • I can't believe it, I started writing an answer then found out I already answered this 2 years ago!. According to my smarter self, Moq 4.2 has a `ThrowsAsync` method. You can write : `.Setup(...).ThrowsAsync(new InvalidOperationException());` – Panagiotis Kanavos Jan 31 '17 at 12:12
  • @user48408 the question was clear from the start to people that use Moq - how can you set up a `Task` method to throw? You also tagged the question with `moq` and even added `Moq` to the title. You couldn't make it any clearer – Panagiotis Kanavos Jan 31 '17 at 12:14
  • @PanagiotisKanavos it wasn't clear at all. The member that is mocked and the member that is called are *different members*. Sure we could assume that's a typo but I don't think you can exclude silly errors like that when the question is explicitly about code that doesn't work. – Ant P Jan 31 '17 at 12:16
  • That was it, so simple..i've done it many times before, don't know why i thought it was more complicated. I can't accept answer in comments if you want to add a response to question i can – user48408 Jan 31 '17 at 12:17
  • @AntP `CountCustomFieldsAsync` is mocked *and* called. What are you talking about? – Panagiotis Kanavos Jan 31 '17 at 12:17
  • @PanagiotisKanavos clientService != mockedService. – Ant P Jan 31 '17 at 12:18
  • @user48408 I already did - that duplicate is mine. I answered the same question almost exactly 3 years and 10 days ago, even updated it for later versions and forgot about it – Panagiotis Kanavos Jan 31 '17 at 12:18
  • @AntP if you want to argue, you can argue that you can't define a controller action inside a test class. It's obvious that one part of the code is the action, the other is the test – Panagiotis Kanavos Jan 31 '17 at 12:19
  • @PanagiotisKanavos no idea what you're talking about - there are two different references and only one of them has a mocked member. The question was about code that doesn't work. Mocking a method on the wrong object was an entirely valid line of inquiry. You clearly didn't read the question properly before you (curtly) shot down my comment. – Ant P Jan 31 '17 at 12:22
  • @AntP you can vote to reopen – Panagiotis Kanavos Jan 31 '17 at 12:24

0 Answers0