Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Moq-如何设置惰性界面_C#_Unit Testing_Mocking_Moq - Fatal编程技术网

C# Moq-如何设置惰性界面

C# Moq-如何设置惰性界面,c#,unit-testing,mocking,moq,C#,Unit Testing,Mocking,Moq,我想模拟一个懒惰的接口,并设置一个返回false的方法 问题是,当我运行测试时,我得到一个NotSupportedException: System.NotSupportedException:'在VB成员中的非虚拟可重写上的设置无效:mock=>mock.Value 以下是一个简化的示例: [TestMethod] public void SomeTestMethod() { var someService = new Mock<Lazy<IService>>(

我想模拟一个懒惰的接口,并设置一个返回false的方法

问题是,当我运行测试时,我得到一个NotSupportedException:

System.NotSupportedException:'在VB成员中的非虚拟可重写上的设置无效:mock=>mock.Value

以下是一个简化的示例:

[TestMethod]
public void SomeTestMethod()
{
    var someService = new Mock<Lazy<IService>>();

    /*this line throws the exception*/  
    someService.Setup(x => x.Value.SomeMethod()).Returns(false);
    ...
}

你从正确的轨道开始

var someService = new Mock<IService>();
var lazySomeService = new Lazy<IService>(() => someService.Object);

这样,当调用Lazy.Value时,它将使用mock。

您从正确的轨道开始使用

var someService = new Mock<IService>();
var lazySomeService = new Lazy<IService>(() => someService.Object);
这样,当调用Lazy.Value时,它将使用mock

someService.Setup(x => x.SomeMethod()).Returns(false);