在对模拟任务断言之前等待 ContinueWith

问题描述 投票:0回答:0

我正在向看起来像这样的方法添加测试:

public class ClassUnderTest {
    public ClassUnderTest(IMyInterface dependency)
    {
        this.dependency = dependency;
    }

    public bool didFault = false;

    void MethodUnderTest()
    {
        var task = dependency.DoSomethingAsync();

        if (task != null)
        {
            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    didFault = true;
                }
            }
         }
     }
}

在测试中,我想测试任务出错时的行为。所以我有这个测试:

[TestCase(TestName = "My test")]
public void MyTestCase()
{
    Mock<IMyInterface> mock = MockRepository.Create<IMyInterface>();
    ClassUnderTest cut = new ClassUnderTest(mock);
    
    mock.Setup(m => m.DoSomethingAsync()).ThrowsAsync(new Exception());
    cut.MethodUnderTest();

    Assert.IsTrue(cut.didFault);
}

问题在我看来是

ContinueWith
Assert之后被称为
。我怎么能等
ContinueWith
完成再断言呢?

moq
© www.soinside.com 2019 - 2024. All rights reserved.