Reputation: 6882
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.
Upvotes: 0
Views: 283
Reputation: 3827
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:
// 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);
Upvotes: 2