开发者

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.

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 t开发者_开发问答he 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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜