为什么我要模拟的属性需要是虚拟的? [英] Why does the property I want to mock need to be virtual?
问题描述
我正在做一些单元测试,并使用 Moq 模拟一些属性.
I'm doing some unit testing, and mocking some properties using Moq.
现在,这是一个控制器测试(ASP.NET MVC 3).我的控制器派生自一个抽象控制器,称为AbstractController.
Now, this is a Controller test (ASP.NET MVC 3). My Controllers derive from an abstract controller, called AbstractController.
此控制器依赖于 Http 上下文(为了执行诸如主题化、基于 HTTP HOST 标头的特定于域的逻辑等).
This controller has a dependency on the Http Context (in order to do things like theming, domain-specific logic based on HTTP HOST headers, etc).
这是通过名为 WebSiteSettings 的属性完成的:
This is done via a property called WebSiteSettings:
public abstract class AbstractController : Controller
{
public WebSiteSettings WebSiteSettings { get; private set; }
// other code
}
注意私有集 - ctor 设置它.所以,我把它改成使用一个接口,这就是我嘲笑的:
Notice the private set - the ctor sets it up. So, i changed it to used an interface, and that's what i've mocked:
public IWebSiteSettings WebSiteSettings { get; private set; }
然后我创建了一个FakeWebSiteSettings",它模拟了 Http 上下文,以便它读取 HTTP 标头.
I then created a "FakeWebSiteSettings", which mocks the Http Context in order for it to read the HTTP headers.
问题是,当我运行测试时,我得到一个 NotSupportedException:
The problem is, when i run the test, i get a NotSupportedException:
非虚拟(在 VB 中可覆盖)成员上的无效设置:x => x.WebSiteSettings
Invalid setup on a non-virtual (overridable in VB) member: x => x.WebSiteSettings
这是相关的模拟代码:
var mockWebSiteSettings = new Mock<FakeWebSiteSettings>();
var mockController = new Mock<MyController>(SomeRepository);
mockController.Setup(x => x.WebSiteSettings).Returns(mockWebSiteSettings.Object);
_controller = mockController.Object;
var httpContextBase = MvcMockHelpers.FakeHttpContext();
httpContextBase.Setup(x => x.Request.ServerVariables).Returns(new NameValueCollection
{
{"HTTP_HOST","localhost.www.mydomain.com"},
});
_controller.SetFakeControllerContext(httpContextBase.Object);
如果我将 WebsiteSettings
属性设为 virtual - 测试通过.
If i make the WebsiteSettings
property virtual - the test passes.
但我不明白为什么我需要这样做.我实际上并没有覆盖这个属性,我只是在嘲笑它是如何设置的.
But i can't understand why i need to do this. I'm not actually overriding the property, i'm simply mocking how it is setup.
我是不是遗漏了什么,或者做错了什么?
Am i missing something, or doing this wrong?
推荐答案
Moq 和其他类似的模拟框架只能模拟接口、抽象方法/属性(在抽象类上)或虚拟方法/属性在具体类上.
Moq and other similar mocking frameworks can only mock interfaces, abstract methods/properties (on abstract classes) or virtual methods/properties on concrete classes.
这是因为它会生成一个代理,该代理将实现接口或创建一个派生类来覆盖那些可覆盖的方法以拦截调用.
This is because it generates a proxy that will implement the interface or create a derived class that overrides those overrideable methods in order to intercept calls.
这篇关于为什么我要模拟的属性需要是虚拟的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!