简体   繁体   English

我可以复制Moq Mock实现吗?

[英]Can I copy a Moq Mock implementation?

I've been using Moq and loving it, but I've come across a problem mocking some legacy code (before I try to refactor it away) 我一直在使用Moq并喜欢它,但我遇到了一个模拟一些遗留代码的问题(在我尝试重构之前)

What I really want to do is have two separate mocks, with only slightly different implementation, such as the sample below. 我真正想要做的是有两个单独的模拟,只有略有不同的实现,如下面的示例。

        Mock<IFoo> fooMock = new Mock<IFoo>();
        fooMock.SetupGet(f => f.bar).Returns(7);
        fooMock.SetupGet(f => f.bar2).Returns(3);
        Mock<IFoo> fooMockClone = new Mock<IFoo>(fooMock.Behavior);
        fooMockClone.SetupGet(f => f.bar).Returns(9);

        Debug.Assert(7 == fooMock.Object.bar);
        Debug.Assert(9 == fooMockClone.Object.bar);
        Debug.Assert(3 == fooMockClone.Object.bar2);
        Debug.Assert(3 == fooMock.Object.bar2);

This is a simple example but the real code is an object with dozens of methods and I want slightly different implementation for two versions. 这是一个简单的例子,但真正的代码是一个有很多方法的对象,我想要两个版本略有不同的实现。

Thanks 谢谢

I wonder in this case then if you're looking for Behavior tests. 我想知道在这种情况下,如果你正在寻找行为测试。 Here's a sample in Machine.Fakes with the Moq sublayer ... it allows for the nesting it seems like you're wanting, while still maintaining logical separation of the tests. 这是使用Moq子层的Machine.Fakes中的示例...它允许嵌套看起来像你想要的,同时仍然保持测试的逻辑分离。 Requires NuGet packages: Machine.Fakes.Moq, Machine.Specifications.Should 需要NuGet包:Machine.Fakes.Moq,Machine.Specifications.Should

class IFoo_test_context : WithSubject<IFoo>
{
    Establish context = () => Subject.bar2 = 3;
}

class When_fooing_with_seven : IFoo_test_context
{
    Establish context = () => Subject.bar = 7;

    It bar_should_be_seven =()=> Subject.bar.ShouldEqual(7);
    It bar2_should_be_three =()=> Subject.bar.ShouldEqual(3);
}

class When_fooing_with_nine : IFoo_test_context
{
    Establish context = () => Subject.bar = 9;

    It bar_should_be_nine = () => Subject.bar.ShouldEqual(9);
    It bar2_should_be_three = () => Subject.bar.ShouldEqual(3);
}

Again the example is somewhat silly because it's testing the mocking behavior, but it's hard to see what you're ultimately trying to accomplish. 再一次,这个例子有些愚蠢,因为它正在测试模拟行为,但很难看出你最终想要完成什么。 There is no copy constructor in the way you want with Mock as far as I can tell. 据我所知,Mock没有你想要的复制构造函数。

You don't mock implementation details, you mock values so that you can get to a point in your code that you need to test. 您不模拟实现细节,您可以模拟值,以便您可以在代码中找到需要测试的点。 There's no point in writing a test that tests whether a mocked dependency outputs a value, then you're just testing moq. 编写一个测试模拟依赖项输出值的测试没有意义,那么你只是测试moq。

Ex: 例如:

public class Stuff {
    IFoo _foo;
    IBar _bar;
    public Stuff(IFoo foo, IBar bar){
        _foo = foo;
        _bar = bar;
    }
    public void DoStuff() {
        if(_foo.HasFooage) {
            _bar.Bar = 4;
        }
    }
}

Then your test would: 然后你的测试会:

...
public void test_bar_does_bar() {
    var foo = new Mock<IFoo>();
    foo.Setup(f => f.HasFooage).Returns(true);
    var bar = new Mock<IBar>();
    var stuff = new Stuff(foo.Object, bar.Object);

    stuff.DoStuff();

    Assert.That(bar.Bar, Is.EqualTo(4));
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM