嘲讽抛出异常(MOQ),但除此之外,像嘲笑的对象的方法? [英] Mocking a method to throw an exception (moq), but otherwise act like the mocked object?

查看:122
本文介绍了嘲讽抛出异常(MOQ),但除此之外,像嘲笑的对象的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传输类,简化它看起来是这样的:

 公共类转移
{
公共虚拟IFileConnection源{搞定;组; }
公共虚拟IFileConnection目的地{搞定;组; }

公共虚拟无效的GetFile(IFileConnection连接,
串remoteFilename,串localFilename)
{
connection.Get(remoteFilename,localFilename);
}

公共虚拟无效PUTFILE(IFileConnection连接,
串localFilename,串remoteFilename)
{
connection.Get(remoteFilename,localFilename);
}

公共虚拟无效TransferFiles(字符串SOURCENAME,串了destname)
{
源= internalConfig.GetFileConnection(源);
=目的地internalConfig.GetFileConnection(目标);
VAR tempName = Path.GetTempFileName();
的GetFile(来源SOURCENAME,tempName);
PUTFILE(目的地,tempName,了destname);
}
}



的简化版本 IFileConnection 界面看起来是这样的:

 公共接口IFileConnection 
{
无效获取(串remoteFileName,串localFileName);
无效将(字符串localFileName,串remoteFileName);
}



真正的类应该处理系统。 IO.IOException IFileConnection 具体类失去连接与远程,向用户发送电子邮件,哪些不是被抛出。



我想用最小起订量来创建一个传输类,并把它作为我的具体转移中的所有属性和方法的类,当调用的GetFile 方法除了 - 那么我希望它抛出一个 System.IO.IOException 并确保传输类处理正确。



我使用权的工具的工作?我要对这个正确的方式?我怎么会写为 NUnit的


解决方案
<那个单元测试设置p>这是我如何设法做什么,我试图做的:

  [测试] 
公共无效TransferHandlesDisconnect ()
{
// ...在这里设立的配置
变种methodTester =新的模拟<&转移GT;(configInfo);
methodTester.CallBase = TRUE;
methodTester.Setup(M => m.GetFile(It.IsAny< IFileConnection>(),It.IsAny<串>(),It.IsAny<串>()))
.Throws< ; System.IO.IOException>();

methodTester.Object.TransferFiles(foo1,foo2的);
Assert.IsTrue(methodTester.Object.Status == TransferStatus.TransferInterrupted);
}

如果有这种方法的问题,我想知道;其他答案建议我这样做不对,但是这正是我试图做的。


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);
    }
}

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);
}

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.

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.

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),但除此之外,像嘲笑的对象的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆