尝试为 WPF 用户控件创建 NUnit 测试



我已经将我的 WPF 项目引用到我创建的 NUnit 测试项目中。我正在尝试调用它的函数,但出现以下编译错误:

Reference to type 'DependencyObject' claims it is defined in 'WindowsBase', but it could not be found

我已经在NUnit测试的"程序集"部分中添加了PresentationFramework,PresentationCore和WindowsBase作为参考,但没有...不知道我做错了什么...代码非常基本...

using NUnit.Framework;
using System.Collections.Generic;
using System.Threading;
using CM;
namespace Tests
{
[TestFixture, Apartment(ApartmentState.STA)]
public class Tests
{
InstUserControl userControlMV = new InstUserControl ();
[Test]
public void Test1()
{
List<string> pOptions = new List<string>() { "2x4", "4x6" };
userControlMV.SetPOptions(pOptions);
Assert.Pass();
}
}
}

任何人都知道我做错了什么。

不能创建NUnit Test Project (.NET Core)来测试面向 .NET Framework 的 WPF 应用程序。这是你应该做的:

  • 创建新Unit Test Project (.NET Framwork)
  • 安装NUnit并将 NuGet 包NUnit3TestAdapter
  • 其中
  • 添加对要测试的 WPF 项目的引用
  • 添加对 Presentation Framework 的引用.dll
  • 添加对 PresentationCore.cll 的引用
  • 添加对 WindowsBase 的引用.dll
  • 运行测试。

最新更新