简体   繁体   English

Moq.MockException:模拟的以下设置不匹配

[英]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]甚至在其检查中包括此类存根属性的事实可能是一个错误(已在SetupAllPropertiesSetupAllProperties )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Moq中苦苦挣扎:以下设置不匹配 - Struggling with Moq: The following setups were not matched Moq.MockException:调用失败,模拟行为严格 - Moq.MockException: Invocation failed with mock behavior Strict 以下设置不匹配,使用Moq进行WPF MVVM单元测试 - The Following Setups were Not Matched, WPF MVVM Unit Testing with Moq 单元测试和最小起订量工作显示异常为:以下设置不匹配: - Unit test and moq work shows exception as: The following setups were not matched: 最小起订量与方法不匹配。 Moq.MockException:模拟上的所有调用都必须具有相应的设置 - Moq doesn't match methods. Moq.MockException: All invocations on the mock must have a corresponding setup Moq.MockException:对模拟的预期调用恰好 1 次,但为 0 次:x =&gt; x.Init() - Moq.MockException: Expected invocation on the mock exactly 1 times, but was 0 times: x => x.Init() Moq.Mock <T> 使用最小起订量将表达式设置到模拟中会导致模拟设置不匹配 - Moq.Mock<T> setting up expressions into a Mock using MOQ results in mock setups not being matched 为什么 XUnit 测试用例会抛出“Moq.MockException:调用失败”错误,而我的输入都是正确的? - Why XUnit Test case throwing "Moq.MockException : invocation failed" error, where my inputs all are correct? MOQ 错误设置与异步/等待单元测试不匹配 - MOQ error setups not matched with Async / Await Unit Test 最小起订量设置和关闭 - Moq setups and closures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM