为什么我的界面上缺少Moq设置方法?

为什么我的界面上缺少Moq设置方法?

问题描述:

我有下面的代码,它通过接口将其预计协议处理的字典列表的功能:为什么我的界面上缺少Moq设置方法?

var _protocolHandlers = new Dictionary<EmailAccountType, IEmailTransportHandler> 
         { 
          {EmailAccountType.Pop3, new Mock<IEmailTransportHandler>().Object}, 
          {EmailAccountType.IMAP, new Mock<IEmailTransportHandler>().Object} 
         }; 

奇怪的是,虽然,下面的代码不给我上的建立方法嘲笑的对象:

_protocolHandlers[0].<where are set-up methods??> 

这似乎遵循传递到服务的构造正常接口公约,因为这些需要的接口,但他们使用的是.Object注射()。

有没有人有线索这里发生了什么?

的安装方法是模拟“容器”,而不是实际的嘲笑对象是什么已经获得通过。

如果您之前创建你的嘲弄,你就可以进入设置,然后通过在对象:

[TestFixture] 
public class MyTest 
{   
    Dictionary<EmailAccountType, IEmailTransportHandler> _protocolHandlers; 
    Mock<IEmailTransportHandler> _mockEmailTransportHander = new Mock<IEmailTransportHandler>();   

    [SetUp] 
    public void Init() 
    { 
     _protocolHandlers = new Dictionary<EmailAccountType, IEmailTransportHandler> 
        { 
         {EmailAccountType.Pop3, _mockEmailTransportHander.Object}, 
         {EmailAccountType.IMAP, _mockEmailTransportHander.Object} 
        }; 
    } 

    [Test] 
    public void Test1() 
    { 
     _mockEmailTransportHander.Setup(m => m.Test()).Returns(false); 
     // Rest of test 
    } 

    [Test] 
    public void Test2() 
    { 
     _mockEmailTransportHander.Setup(m => m.Test()).Returns(true); 
     // Rest of test 
    } 
} 
+0

嗨,谢谢你。我的_protocolHandlers将在NUnit TextFixture :: Setup()中实例化。我需要调用每次测试中的设置(例如,在字典对象上设置模拟),但是从您的示例看来,这太迟了......? – jaffa

+0

@jaffa我已经更新了我的答案,它将如何与nunit一起工作 –

+0

嗨,非常感谢!我使用原来的参考,然后用于添加到字典中。仍然无法解决为什么我无法访问字典。对象虽然?!?我相信便士会下降;) – jaffa