如何模拟Application.Current进行单元测试

本文关键字:Current 单元测试 Application 何模拟 模拟 | 更新日期: 2023-09-27 18:25:56

我有这个:

<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为空,所以pack://application:,,,/...不指向任何地方),并出现以下错误:

Assembly.GetEntryAssembly() returns null. Set the Application.ResourceAssembly property or use the pack://application:,,,/assemblyname;component/ syntax to specify the assembly to load the resource from.

如何模拟/注入解决问题所需的内容?

如何模拟Application.Current进行单元测试

Tal的回答对我不起作用,我只是在运行我的测试和应用程序之前打电话给下面。当前已填充:

var app = new Application();

好的,明白了,感谢Will:

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