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

时间:2013-08-01 08:48:09

标签: c# wpf unit-testing mocking mstest

我有这个:

<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:,,,/...指向无处)并出现此错误:< / p>

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.

如何模拟/注入解决它所需的一切?

2 个答案:

答案 0 :(得分:3)

好的,得到它,感谢Will

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

var window = new MainWindow();

答案 1 :(得分:3)

Tal的答案对我没有用,我只是在运行测试之前调用下面的Application.Current填充:

var app = new Application();