[英]Stub a property which is in the concrete class but not on the interface for unit testing
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. 我现在想要基于该接口实现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;
}
}
My SUT gets a IResolver per constructor injection loaded with my mefViewResolver. 我的SUT获取一个加载了mefViewResolver的构造函数注入的IResolver。 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.
在我的单元测试中,我想从外部预先设置我的Views属性,而不使用mef或在我的界面中特定于mef。 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...
基本上我想用期望值设置视图,看看我使用IViewResolver的viewmodel是否返回预设视图......如果我的界面上不存在views属性,我怎样才能存根...
If I am on the wrong path... any corrections would much appriciated.. 如果我在错误的道路上...任何更正都会很有帮助...
Thanks D. 感谢:D。
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
. 如果你想测试只知道IViewResolver接口的ViewModel(而不是Resolver),你应该没有任何问题:ViewModel可以访问的唯一方法(根据提供的代码)是
GetViewFor
。 All you need to do is return the appropriate View for each test case, given the View name. 您需要做的就是在给定View名称的情况下为每个测试用例返回相应的View。 In RhinoMocks it should be something like:
在RhinoMocks中它应该是这样的:
// Arrange the test objects
var viewResolverMock = MockRepository.GenerateMock<IViewResolver>();
viewResolverMock.Stub(x => x. GetViewFor(thisTestViewName).Return(thisTestView);
var myViewModel = new MyViewModel(viewResolverMock);
// Do the actual operation on your tested object (the view model)
var actualResult = myViewModel.DoSomethingWithTheView();
// Assert
AssertAreEqual(expectedResult, actualResult);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.