模拟一个抛出异常(moq)的方法,但在其他方面表现得像被模拟的对象?

时间:2023-02-26
本文介绍了模拟一个抛出异常(moq)的方法,但在其他方面表现得像被模拟的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 Transfer 类,简化后如下所示:

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

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

    public virtual void GetFile(IFileConnection connection, 
        string remoteFilename, string localFilename)
    {
        connection.Get(remoteFilename, localFilename);
    }

    public virtual void PutFile(IFileConnection connection, 
        string localFilename, string remoteFilename)
    {
        connection.Get(remoteFilename, localFilename);
    }

    public virtual void TransferFiles(string sourceName, string destName)
    {
        source = internalConfig.GetFileConnection("source");
        destination = internalConfig.GetFileConnection("destination");
        var tempName = Path.GetTempFileName();
        GetFile(source, sourceName, tempName);
        PutFile(destination, tempName, destName);
    }
}

IFileConnection接口的简化版如下所示:

The simplified version of the IFileConnection interface looks like this:

public interface IFileConnection
{
    void Get(string remoteFileName, string localFileName);
    void Put(string localFileName, string remoteFileName);
}

真正的类应该处理 System.IO.IOExceptionIFileConnection 具体类失去与远程的连接、发送电子邮件等时抛出的.

The real class is supposed to handle a System.IO.IOException that is thrown when the IFileConnection concrete classes loses connectivity with the remote, sending out emails and what not.

我想使用 Moq 创建一个 Transfer 类,并在所有属性和方法中将它用作我的具体 Transfer 类,除了 GetFile 方法被调用 - 然后我希望它抛出一个 System.IO.IOException 并确保 Transfer 类正确处理它.

I would like to use Moq to create a Transfer class, and use it as my concrete Transfer class in all properties and methods, except when the GetFile method is invoked - then I want it to throw a System.IO.IOException and make sure the Transfer class handles it properly.

我是否使用了正确的工具来完成这项工作?我会以正确的方式解决这个问题吗?我将如何为 NUnit 编写该单元测试的设置?

Am I using the right tool for the job? Am I going about this the right way? And how would I write the setup for that unit test for NUnit?

推荐答案

这就是我设法做我想做的事情的方式:

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<Transfer>(configInfo);
    methodTester.CallBase = true;
    methodTester
        .Setup(m => 
            m.GetFile(
                It.IsAny<IFileConnection>(), 
                It.IsAny<string>(), 
                It.IsAny<string>()
            ))
        .Throws<System.IO.IOException>();

    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.

这篇关于模拟一个抛出异常(moq)的方法,但在其他方面表现得像被模拟的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:TDD 和模拟 TcpClient 下一篇:如何在单元测试中起订量网络流?

相关文章

最新文章