Mocking a method to throw an exception (moq), but otherwise act like the mocked object?

前端 未结 3 767
天涯浪人
天涯浪人 2020-12-15 02:38

I have a Transfer class, simplified it looks like this:

public class Transfer
{
    public virtual IFileConnection source { get; set; }
    publ         


        
3条回答
  •  一生所求
    2020-12-15 03:05

    This is how I managed to do what I was trying to do:

    [Test]
    public void TransferHandlesDisconnect()
    {
        // ... set up config here
        var methodTester = new Mock(configInfo);
        methodTester.CallBase = true;
        methodTester
            .Setup(m => 
                m.GetFile(
                    It.IsAny(), 
                    It.IsAny(), 
                    It.IsAny()
                ))
            .Throws();
    
        methodTester.Object.TransferFiles("foo1", "foo2");
        Assert.IsTrue(methodTester.Object.Status == TransferStatus.TransferInterrupted);
    }
    

    If there is a problem with this method, I would like to know; the other answers suggest I am doing this wrong, but this was exactly what I was trying to do.

提交回复
热议问题