1

我知道测试非公共成员被认为是不好的,但为什么这不起作用?它抛出ArgumentException消息:成员Connect不存在

public class FtpHelper : IFtpHelper
{
   public FtpHelper(ISignalRLogger signalRLogger, IDirectoryWrap directoryWrap = null) {
    ... some code
   }

   protected virtual IFtpClient Connect(string ftpPath) {
     ... some code
   }
}

public class TestFtpHelper
{
   [Fact]
   public void Calls_connect(){

      var signalrMq = new Mock<ISignalRLogger>();
      var clientMq = new Mock<FtpHelper>(
            MockBehavior.Strict, new { signalRLogger = signalrMq.Object });

      clientMq.Protected().Setup<IFtpClient>("Connect",new { ftpPath = ""})
    .Returns(It.IsAny<IFtpClient>()); // That doesn't work

   }
}

UPD:哦,我注意到,即使在它初始化的最后一行之前clientMq,我尝试使用clientMq.Object它也会抛出异常,并显示以下消息:在模拟类型上找不到给定参数的匹配构造函数。我想这与那个带有默认值的奇怪构造函数有关

4

2 回答 2

3

您需要以下附加设置
1.using Moq.Protected()在顶部添加 2. 使用属性
使类型对 Moq 可见。InternalsVisibleTo[程序集:InternalsVisibleTo(RhinoMocks.NormalName)] [程序集:InternalsVisibleTo(RhinoMocks.StrongName)]

链接

于 2013-01-09T18:04:03.000 回答
0

我明白出了什么问题......
这个:

var clientMq = new Mock<FtpHelper>(
            MockBehavior.Strict, new { signalRLogger = signalrMq.Object });

应该:

var clientMq = new Mock<FtpHelper>(
            MockBehavior.Strict, signalrMq.Object);

签名是params object[]

但现在它抛出NullReferenceException

Upd:你试图模拟的成员不仅应该,virtual而且还internal'. Making them 'protected不够。无法识别代理Refection.Emit(),Moq 使用它/

于 2013-01-09T18:22:51.410 回答