[英]Moq.MockException: The following setups on mock were not matched
In below code i am using Moq
to write a sample test. 在下面的代码中,我正在使用
Moq
编写示例测试。 I have created a Mock Object and I am using SetupProperty
to setup fake value to be returned for the property. 我已经创建了一个模拟对象,并且正在使用
SetupProperty
设置要为该属性返回的假值。 But i get above error at line _sharedService.VerifyAll()
. 但是我在
_sharedService.VerifyAll()
行上_sharedService.VerifyAll()
以上错误。
I know i am missing something trivial, but not exactly sure what. 我知道我缺少一些琐碎的东西,但不确定是什么。 Could anyone please help?
谁能帮忙吗?
[TestFixture]
public class ObjectFactoryTests : TestFixtureBase
{
private Mock<ISharedService> _sharedService;
[SetUp]
public void SetUp()
{
_sharedService = new Mock<ISharedService>(MockBehavior.Strict);
}
protected override void VerifyAll()
{
_sharedService.VerifyAll();
}
private IObjectFactory GetObjectFactory()
{
return new ObjectFactory(sharedService.Object);
}
[Test]
public void ObjectFactory_GenerateObject_Request_Success()
{
MyObject1 request = something;
var requestData = new Dictionary<string, object>();
requestData.TryAdd(Cache.Client, Constants.CLIENT);
_sharedService.SetupProperty(m => m.RequestData, requestData);
var factory = GetObjectFactory();
var actual = factory.GenerateObject(request);
Assert.That(actual.Client, Is.EqualTo(requestData[Cache.Client].ToString()), Constants.CLIENT);
VerifyAll();
}
}
public class ObjectFactory : IObjectFactory
{
ISharedService SharedService = something;
public MyObject GenerateObject(MyObject1 request)
{
MyObject obj = new MyObject(request);
obj.Client = SharedService.RequestData[Cache.Client].ToString();
return obj;
}
}
If I understood correctly, you try to setup property expectations. 如果我理解正确,则您尝试设置属性期望值。
Try following instead of _sharedService.SetupProperty(m => m.RequestData, requestData);
尝试以下方法代替
_sharedService.SetupProperty(m => m.RequestData, requestData);
: :
_sharedService.Setup(foo => foo.RequestData).Returns(requestData);
You can read more information in Moq documentation 您可以在Moq文档中阅读更多信息
For a get-set peoperty, SetupProperty
will create two setups: one for the getter, and one for the setter. 对于get-set peoperty,
SetupProperty
将创建两个设置:一个用于getter,一个用于setter。 Since you're only reading the property, that leaves the property for the setter unmatched, therefore the error. 由于您只读取属性,因此设置器的属性不匹配,因此会出现错误。
To avoid this, use mock.SetupGet(m => m.Property).Returns(() => value)
to only create a setup for the getter. 为了避免这种情况,请使用
mock.SetupGet(m => m.Property).Returns(() => value)
只为吸气剂创建一个设置。
Btw.: SetupProperty
actually has a different purpose than what you might think: It shouldn't be used to set up an expectation; 顺便说一句:
SetupProperty
实际上具有与您可能想到的目的不同的目的:不应将其用于建立期望; instead, it is used to "stub" a property such that it retains the value it's been set to last. 取而代之的是,它用于“存根”属性,以使其保留设置为last的值。 The fact that
Verify[All]
even includes such stubbed properties in its checks is probably an error (which has already been fixed in SetupAllProperties
). Verify[All]
甚至在其检查中包括此类存根属性的事实可能是一个错误(已在SetupAllProperties
中SetupAllProperties
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.