您可以使用Moq.MockRepository.Of()
通過表達式創建模擬。這可以通過一個例子來解釋:
public interface ITest
{
String Say();
Int32 DoSomething(Int32 a, Int32 b);
}
class Program
{
public static void Main()
{
var repo = new MockRepository(MockBehavior.Strict);
var mock = repo.Of<ITest>()
.Where(s => s.Say() == "Hi!")
.Where(s => s.DoSomething(5, 4) == 20)
.Where(s => s.DoSomething(4, 5) == 9)
.Where(s => s.DoSomething(It.IsAny<Int32>(), 7) == 99)
.First();
Console.WriteLine(mock.Say()); // prints Hi!
Console.WriteLine(mock.DoSomething(5, 4)); // prints 20
Console.WriteLine(mock.DoSomething(4, 5)); // prints 9
Console.WriteLine(mock.DoSomething(23423, 7)); // prints 99
Console.WriteLine(mock.DoSomething(0, 0)); // Fail due to MockBehavior.Strict
Console.ReadKey();
}
}
我很難得到'ITest'模擬的配置實例。示例代碼按原樣工作,但否則獲取'mock'實例會導致'MockException:模擬的所有調用都必須具有相應的設置「。 或 'var mock = repo.Of().First();' 我想我做錯了什麼? –