存根位于具体类中但不在接口上的属性以进行单元测试

发布于 2024-12-09 14:19:52 字数 800 浏览 0 评论 0原文

我有一堂课,其中的信息比我的界面更多。它有一个我没有在界面中公开的属性。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-12-16 14:19:52

如果你想测试你的ViewModel(而不是Resolver),它只知道IViewResolver接口,你不应该有任何问题:ViewModel可以访问的唯一方法(根据提供的代码)是GetViewFor。您需要做的就是根据视图名称,为每个测试用例返回适当的视图。在 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文