对具有 MEF 导入值的类进行单元测试

发布于 2024-09-17 21:28:50 字数 768 浏览 4 评论 0原文

我有一个名为“ViewFactory”的类,该类现在应该提供正确的视图,

它只有一个方法(并且它会增长),我想针对该方法编写单元测试。

该类看起来像这样...

  public class ViewFactory
{

       [ImportMany(AllowRecomposition=true)]
    IEnumerable<ExportFactory<DependencyObject, IViewMetaData>> Views { get; set; }

    public DependencyObject GetViewByName(string name)
    {
        DependencyObject view = null;

        try
        {
            view = Views.Where(v => v.Metadata.ViewName == name).FirstOrDefault().CreateExport().Value;
            return view;
        }
        catch (Exception ex)
        {

            return view;
        }
    }
}

我真正想要的是测试我的方法,但不知道该怎么做,因为视图列表是在运行时组成的...

我想测试我是否获得有效的视图姓名 和 我还想测试如果我的名称无效,我是否会得到 null

正确的方法是什么?

I have a a class called "ViewFactory" and this class should deliver the right view

right now it has only one method (and it will grow) which I want to write a unit test against.

the class looks like this...

  public class ViewFactory
{

       [ImportMany(AllowRecomposition=true)]
    IEnumerable<ExportFactory<DependencyObject, IViewMetaData>> Views { get; set; }

    public DependencyObject GetViewByName(string name)
    {
        DependencyObject view = null;

        try
        {
            view = Views.Where(v => v.Metadata.ViewName == name).FirstOrDefault().CreateExport().Value;
            return view;
        }
        catch (Exception ex)
        {

            return view;
        }
    }
}

what I do want is to test my method but don't know how to do it because the List of Views is composed on runtime...

I want to test if I get a view for a valid name
and
I also want to test if I get null if I have an invalid name

What would be the right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一笑百媚生 2024-09-24 21:28:50

您将为 ViewFactory 提供一组适合特定测试的 ExportFactory<,> 值。不同的测试可能有不同的集合,以允许您测试不同的东西。基本上你是在伪造注入的依赖关系。

You would provide your ViewFactory with a set of ExportFactory<,> values suitable for the particular test. Different tests might have different sets, to allow you to test different things. Basically you're faking the injected dependency.

铃予 2024-09-24 21:28:50

这是代码(针对 Silverlight)

var viewFactory = new ViewFactory();

// Satisfy the imports manually (No MEF in unit tests!)
var factories = new List<ExportFactory<DependencyObject, IViewMetaData>>();
var tuple = new Tuple<DependencyObject, Action>(new MyObject(), () => { });
var metaData = new MyViewMetaData();
var factory = new ExportFactory<DependencyObject, IViewMetaData>(() => tuple, metaData);
factories.Add(factory);
viewFactory.Views = factories;

Here's the code (for Silverlight)

var viewFactory = new ViewFactory();

// Satisfy the imports manually (No MEF in unit tests!)
var factories = new List<ExportFactory<DependencyObject, IViewMetaData>>();
var tuple = new Tuple<DependencyObject, Action>(new MyObject(), () => { });
var metaData = new MyViewMetaData();
var factory = new ExportFactory<DependencyObject, IViewMetaData>(() => tuple, metaData);
factories.Add(factory);
viewFactory.Views = factories;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文