如何模拟Application.Current单元测试? [英] How to mock Application.Current for unit-testing?

查看:530
本文介绍了如何模拟Application.Current单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:

<Image.Effect>
    <fx:GrayscaleEffect DesaturationFactor="0"/>
</Image.Effect>

和这样的:

public class GrayscaleEffect : ShaderEffect{
    private static PixelShader _pixelShader = new PixelShader()
        {
            UriSource = new Uri(@"pack://application:,,,/Effects/GrayscaleEffect.ps")
        };
    /* ... rest of the class ... */
}



的休息

在我的单元测试( MSTest的),这显然引起了 IOException异常(因为 Application.Current 为空,那么包://应用:,,, / ... 指向无处)与此错误:

When I unit-test it (MSTest), it obviously raises IOException (since Application.Current is null, so pack://application:,,,/... points to nowhere) with this error:

Assembly.GetEntryAssembly()返回null。设置Application.ResourceAssembly属性或使​​用pack://应用:,,, /的AssemblyName;组件/语法来指定程序集加载从资源

我如何模拟/注入任何需要解决呢?

How do I mock/inject whatever needed to resolve it ?

推荐答案

好,知道了,感谢的:

if(Application.ResourceAssembly == null)
    Application.ResourceAssembly = typeof(MainWindow).Assembly;

var window = new MainWindow();

这篇关于如何模拟Application.Current单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆