1

我试图测试一个用 C# 编写的需要 .NET 3.5 的 POS 驱动程序。我将 NUnit 用于 VS17,因为我有一个用 C# 编写的控制台应用程序代码,并对驱动程序进行了一些功能测试。

它工作得很好,但是当我尝试从 NUnit 调用相同的方法时,它会引发异常。

这是工作代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FiscalReceipt.Library;

namespace FiscalReceiptMain
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("Setting [STAThread] ");
            FiscalReceipt.Library.FiscalReceipt mc = new FiscalReceipt.Library.FiscalReceipt();
            int output = mc.testFiscalReceiptClass();
            Console.WriteLine("Number of exception = {0}", output);
            System.Environment.Exit(0);
        }
    }
}

问题当然在内部testFiscalReceiptClass

namespace FiscalReceipt.Library
{
    public class FiscalReceipt
    {
        private PosExplorer posExplorer;
        private PosCommon posCommonFP;

        // Exception counter
        public static int NumExceptions = 0;
        public FiscalReceipt()
        {
            posExplorer = null;
            posCommonFP = null;
        }

        // Method to test the original FiscalReceiptClass
        public int testFiscalReceiptClass()
        {
            try
            {
                // Console.WriteLine("Initializing PosExplorer ");
                posExplorer = new PosExplorer();
            }
            catch (Exception e)
            {
                // ...    
            }
        }
    }
}

如果我posExplorer从控制台应用程序创建对象没有问题,但是当我尝试调试 NUnit 时会引发异常:

{"'Microsoft.PointOfService.Management.Explorer' 类型初始化程序引发异常。"} InnerException {"此方法显式使用 .NET Framework 中过时代码访问的安全策略。为了兼容性目的,启用来自代码访问的安全策略,使用 NetFx40_LegacySecurityPolicy 配置选项。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=155570。"} System.Exception { System.NotSupportedException}

在此处输入图像描述

Nunit 似乎是一个与我的 DLL 不兼容的 64 位库。我花了超过 24 个工作小时来尝试许多解决方案,但我现在被锁定了。

建议?

4

0 回答 0