简体   繁体   English

存储具体类中的属性,但不在单元测试的接口上

[英]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.

相关问题 如何存根具体 class 的 ReadOnly 属性 - How to Stub a ReadOnly property of a concrete class 通过具体的类或接口对ASP.NET MVC Controller方法进行单元测试 - Unit testing ASP.NET MVC Controller methods through concrete class or interface 继承通用接口的具体类 - Concrete class which inherites a generic interface 如何使用犀牛对属性进行存根以进行单元测试 - how to stub a property out using rhino for unit testing Automapper - 映射到接口类型属性而不创建具体类 - Automapper - map to Interface type property without creating concrete class 设置在具体类中的接口中定义的只读属性 - Set a read only property defined in a interface within a concrete class C# 反射从接口获取混凝土 class 的 static 属性 - C# Reflection Getting static property of concrete class from Interface 是否可以为创建具体类而创建存根? - Is it possible to create a stub for a creation of a concrete class? 如何为包含只读成员的接口创建单元测试存根? - How do you create a unit-testing stub for an interface containing a read-only member? 是什么原因导致不允许派生类的具体属性实现接口的接口属性? - Whats the reasoning behind not allowing a derived class's concrete property that implements an interface's interface property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM