如何使用 Moq 模拟对具体对象的函数调用? [英] How to mock a function call on a concrete object with Moq?

查看:15
本文介绍了如何使用 Moq 模拟对具体对象的函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Moq 中做到这一点?

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.

在 TypeMock 中,我只需要 Isolate.WhenCalled(bar.PrivateGetter).Returns('whatever value').

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

有什么想法吗?

推荐答案

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

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

这篇关于如何使用 Moq 模拟对具体对象的函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆