存根位于具体类中但不在接口上的属性以进行单元测试
我有一堂课,其中的信息比我的界面更多。它有一个我没有在界面中公开的属性。
public interface IViewResolver
{
object GetViewFor(string viewName);
}
我现在想基于该接口实现 MefViewResolver。
public class ViewResolver : IViewResolver
{
[ImportMany]
public IEnumerable<Lazy<IView,IViewMetaData>> Views { get; set; }
public object GetViewFor(string viewName)
{
var view = Views.Where(x => x.Metadata.Name == viewName).FirstOrDefault();
return view == null ? null : view.Value;
}
}
我的 SUT 在每次构造函数注入时都会获得一个 IResolver,其中加载了我的 mefViewResolver。在我的单元测试中,我想从外部预先设置我的 Views 属性,而不使用 mef 或在我的界面中指定 mef 。 基本上我想设置具有预期值的视图,并查看使用 IViewResolver 的视图模型是否返回预设视图... 即使我的界面上不存在视图属性,我怎样才能存根它...
如果我走错了路...任何更正都会非常感激..
谢谢D。
I have a class which has more information then my inteface. It has a property which I did not expose in my interface.
public interface IViewResolver
{
object GetViewFor(string viewName);
}
I want now to implement a MefViewResolver based on that interface.
public class ViewResolver : IViewResolver
{
[ImportMany]
public IEnumerable<Lazy<IView,IViewMetaData>> Views { get; set; }
public object GetViewFor(string viewName)
{
var view = Views.Where(x => x.Metadata.Name == viewName).FirstOrDefault();
return view == null ? null : view.Value;
}
}
My SUT gets a IResolver per constructor injection loaded with my mefViewResolver. In my unit test I would like to pre-set my Views property from the outside without using mef or being mef specific in my interface.
Basically I want to set the Views with an expected value and see if my viewmodel which uses the IViewResolver returns the preset view...
How can I stub the views property even if it does not exists on my interface...
If I am on the wrong path... any corrections would much appriciated..
Thanks D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想测试你的ViewModel(而不是Resolver),它只知道IViewResolver接口,你不应该有任何问题:ViewModel可以访问的唯一方法(根据提供的代码)是
GetViewFor
。您需要做的就是根据视图名称,为每个测试用例返回适当的视图。在 RhinoMocks 中它应该是这样的:If you want to test your ViewModel (and not the Resolver), which is only aware of the IViewResolver interface, you shouldn't have any problem: the only method (according to the code provided) that the ViewModel can access is
GetViewFor
. All you need to do is return the appropriate View for each test case, given the View name. In RhinoMocks it should be something like: