单元测试:表达式不能包含使用可选参数的调用或调用

时间:2014-08-04 23:55:14

标签: c# mocking moq installation

我有以下内容:

public interface IAction
{
   void Do(string a, int b = 0);
}

public class A : IAction
{
     public void Do(string a, int b = 0)
     {
         // work is done
     }
}
public class B
{
     private readonly IAction _a;
     public B(IAction a) //assume IOC is configured to inject an instance of B 
     {
       _a = a;
     }

     //this is the method i want to test.
     public void DoStuff(string arg)
     {
       //I am calling Do() with ONLY ONE parameter - because the other is optional
       _a.Do(arg); 
       //do something else - optional
     }
}

我的测试看起来像这样:

[TestClass]
 public class BTest
 {
    [TestMethod]
    public void DoStuffShouldBlablaForValidInput()
    { 
       Mock<IAction> _mockedB = new Mock<IAction>(MockBehavior.Strict);
       var b = new B(_mockedB.Object);
       _mockedB .Setup(x => x.Do(It.IsAny<string())).Verifiable();
       b.DoStuff("sample");       
       // verify that Do() was called once
       _mockedB.Verify(x => x.Do(It.IsAny<string()), Times.Once); 
    }
 }

但是我得到了:&#34;表达式不能包含使用可选参数的调用或调用&#34;此行上的错误&#34; _mockedB .Setup(x =&gt; x.Do(It.Any x.Do(It.IsAny)

如何修复此问题而不需要方法DoStuff()传递可选参数以及方法Do()?

谢谢,

0 个答案:

没有答案