NUnit测试项目无法为System.Windows.Forms加载程序集

时间:2019-05-31 20:28:10

标签: c# .net-core visual-studio-2017 nunit

我有一个专门用于单元测试的NUnit测试项目,该项目的目标框架为.Net Core 2.1。该测试项目引用了另外两个项目,其中一个是基于 Windows Form 的应用程序,另一个是基于 Class Library 的用于数据库交互的应用程序。这两个项目都具有.Net Framework 4.6.1的目标框架

我正在编写一个测试,用于测试基于 Class Library 的应用程序的方法,以测试该方法的输出。但它显示以下错误,

'System.IO.FileNotFoundException:无法加载文件或程序集'System.Windows.Forms,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089'。系统找不到指定的文件。'

我尝试了以下修复程序,但没有一个起作用。

  • 删除项目引用,然后再次添加
  • 将构建平台目标从AnyCPU更改为X86和x64
  • 在.config文件中添加相关程序集标记。 (有趣的是,.NET Core 2.1没有提供.config文件)

这是测试类(Test_DataClasses.cs)

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Data.SQLite;
using SharedDataClasses;
using MantechAutomationControl;
using System.IO;
using System.Reflection;

namespace MAC_Testing
{
[TestFixture]
class Test_DataClasses
{
    #region Tests
    [Test]
    public void t_m_initializeDatabase()
    {
        //Assign
        string v_dbLocation = Path.Combine("M:\\For_Jabed\\MAC\\MAC\\MantechAutomationControl\\bin\\Debug\\", "DB.sqlite");
        //Act
        DataClasses_v2.m_initializeDatabase();
        //Assert
        Assert.That(new FileInfo(v_dbLocation), Does.Exist);
    }
    #endregion
}
}

这是上述测试试图验证的方法

public static class DataClasses_v2
{
    #region Variables 
    #endregion
    #region Methods
    public static void  m_initializeDatabase()
    {
        string v_executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        string v_dbLocation = Path.Combine(v_executableLocation, "DB.sqlite");
        if (!File.Exists(v_dbLocation))
        {
            DialogResult o_createDBFileDialogBox = MessageBox.Show("Could not find the database in the expected location:\n" + v_dbLocation + "\n\nWould you like to create a new blank database file?", "Warning", MessageBoxButtons.YesNo);
            if (o_createDBFileDialogBox == DialogResult.Yes)
            {
                SQLiteConnection.CreateFile(v_dbLocation);
                using (SQLiteConnection o_dbConnection = new SQLiteConnection(@"DataSource=" + v_dbLocation + ";Version=3;"))
                {
                    o_dbConnection.Open();
                }
            }
            else
                return;
        }
    }

我不太了解这里发生了什么。尽管需要指出的是,实际上我实际上是在尝试使用 Nunit Form dll与Fomrs(DialogBox)一起工作,因为要测试的方法可以使用DialogBox。

在测试项目中,还有另一个类,并且在这些类中编写的测试对于基于相同目标“类库”的项目可以正常工作,以验证其他功能。

希望有人能够解释这里发生的事情。

0 个答案:

没有答案