如何使用 Moq 模拟具体对象上的函数调用?

发布于 2024-08-25 21:29:06 字数 298 浏览 7 评论 0原文

我怎样才能在最小起订量中做到这一点?

Foo bar = new Foo();
Fake(bar.PrivateGetter).Return('whatever value')

看来我只能找到如何模拟通过框架创建的对象。我只想模拟我创建的具体对象上的单个方法/属性。

在 TypeMock 中,我只需执行 Isolate.WhenCalled(bar.PrivateGetter).Returns('whatever value') 即可。

有什么想法吗?

How can I do this in Moq?

Foo bar = new Foo();
Fake(bar.PrivateGetter).Return('whatever value')

It seems I can only find how to mock an object that was created via the framework. I want to mock just a single method/property on a concrete object I've created.

In TypeMock, I would just do Isolate.WhenCalled(bar.PrivateGetter).Returns('whatever value').

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲在坟头点根烟 2024-09-01 21:29:06

您应该使用 Moq 创建 Mock 对象并将 CallBase 属性设置为 true 以使用对象行为。

来自起订量文档:
CallBase 被定义为“如果没有期望覆盖成员,则调用基类实现。这称为“部分模拟”。它允许模拟类的某些部分,而不必模拟所有内容。

示例代码:

    [Test]
    public void FailintgTest()
    {
        var mock = new Moq.Mock<MyClass>();
        mock.Setup(m => m.Number).Returns(4);
        var testObject = mock.Object;
        Assert.That(testObject.Number, Is.EqualTo(4));
        Assert.That(testObject.Name, Is.EqualTo("MyClass"));
    }

    [Test]
    public void OKTest()
    {
        var mock = new Moq.Mock<MyClass>();
        mock.Setup(m => m.Number).Returns(4);
        mock.CallBase = true;
        var testObject = mock.Object;
        Assert.That(testObject.Number, Is.EqualTo(4));
        Assert.That(testObject.Name, Is.EqualTo("MyClass"));
    }

    public class MyClass
    {
        public virtual string Name { get { return "MyClass"; } }

        public virtual int Number { get { return 2; } }
    }

You should use Moq to create your Mock object and set CallBase property to true to use the object behavior.

From the Moq documentation:
CallBase is defined as “Invoke base class implementation if no expectation overrides the member. This is called “Partial Mock”. It allows to mock certain part of a class without having to mock everything.

Sample code:

    [Test]
    public void FailintgTest()
    {
        var mock = new Moq.Mock<MyClass>();
        mock.Setup(m => m.Number).Returns(4);
        var testObject = mock.Object;
        Assert.That(testObject.Number, Is.EqualTo(4));
        Assert.That(testObject.Name, Is.EqualTo("MyClass"));
    }

    [Test]
    public void OKTest()
    {
        var mock = new Moq.Mock<MyClass>();
        mock.Setup(m => m.Number).Returns(4);
        mock.CallBase = true;
        var testObject = mock.Object;
        Assert.That(testObject.Number, Is.EqualTo(4));
        Assert.That(testObject.Name, Is.EqualTo("MyClass"));
    }

    public class MyClass
    {
        public virtual string Name { get { return "MyClass"; } }

        public virtual int Number { get { return 2; } }
    }
夜深人未静 2024-09-01 21:29:06

只有 TypeMock Isolator(也许还有 Moles)可以执行这些特技。普通动态模拟库可以 仅模拟虚拟和抽象成员

Only TypeMock Isolator (and perhaps Moles) can perform these stunts. Normal dynamic mock libraries can only mock virtual and abstract members.

我很OK 2024-09-01 21:29:06

只要签名上的类型可见,鼹鼠也可以替换私有方法。因此,在这种情况下,它看起来像这样:

MFoo bar = new MFoo { // instantiate the mole of 'Foo'
    PrivateGetterGet = () => "whatever value" // replace PrivateGetter {get;}
};
Foo realBar = bar; // retrive the runtime instance
...

如果您正在寻找有关 Moles 的更多信息,请从 http://research.microsoft.com/en-us/projects/pex/documentation.aspx

Moles can also replace private methods as long as the types on the signature are visible. So in this case, it would look like this:

MFoo bar = new MFoo { // instantiate the mole of 'Foo'
    PrivateGetterGet = () => "whatever value" // replace PrivateGetter {get;}
};
Foo realBar = bar; // retrive the runtime instance
...

If you are looking for more information on Moles, start with the tutorials at http://research.microsoft.com/en-us/projects/pex/documentation.aspx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文