简体   繁体   English

NUnit 测试错误:无法从程序集 'System.Web,版本 = 4.0.0.0 加载类型 'System.Web.HttpApplication'

[英]NUnit Test Error: Could not load type 'System.Web.HttpApplication' from assembly 'System.Web, Version=4.0.0.0

I have tried to search for this but solutions provided don't seem to work for me.我试图搜索这个,但提供的解决方案似乎对我不起作用。 I have an existing C# Class Library project targeting.Net Framework 4.5.1 that is currently live.我有一个现有的 C# Class 库项目,目标是当前正在运行的.Net Framework 4.5.1。 I would like to introduce Unit testing using NUnit.我想介绍使用 NUnit 进行单元测试。 I therefore added NUnit Test Project(.Net Core 3.1) to the solution.因此,我在解决方案中添加了 NUnit Test Project(.Net Core 3.1)。 I have added a reference to the.Net Framework 4.5.1 in the NUnit project, after building it looks fine.我在 NUnit 项目中添加了对 .Net Framework 4.5.1 的引用,构建后看起来不错。 However, I have created a simple test below, when I run I get the error但是,我在下面创建了一个简单的测试,当我运行时出现错误

Test
   Duration: 208 ms

  Message: 
    System.TypeLoadException : Could not load type 'System.Web.HttpApplication' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
  Stack Trace: 
    RuntimeAssembly.GetExportedTypes()
    Assembly.get_ExportedTypes()
    ExtensionsForAssembly.GetNinjectModules(Assembly assembly)
    <>c.<Load>b__25_0(Assembly asm)
    SelectManySingleSelectorIterator`2.ToList()
    Enumerable.ToList[TSource](IEnumerable`1 source)
    KernelBase.Load(IEnumerable`1 m)
    KernelBase.Load(IEnumerable`1 assemblies)
    ModuleLoadExtensions.Load(IKernel kernel, Assembly[] assemblies)
    DirectRewardsController.ctor() line 24
    DirectRewardsTests.Test() line 18

NUnit Test NUnit 测试

public class DirectRewardsTests
    {
        [SetUp]
        public void SetUp()
        {

        }

        [Test]
        public void Test()
        {
            //Arrange
            DirectRewardsController directRewards = new DirectRewardsController(); 
            //Act

            //Assert
            Assert.Pass();
        }
    }

DirectRewardsController直接奖励控制器

public class DirectRewardsController : ApiController
    {
        StandardKernel DependencyKernel;
        
        private IDirectRewards justRewards;
        public DirectRewardsController()
        {
            DependencyKernel = new StandardKernel();
            DependencyKernel.Load(Assembly.GetExecutingAssembly());
            justRewards = DependencyKernel.Get<IDirectRewards>();
        }
        
         public HttpResponseMessage AddMember(MemberRequest model)
        {
            try
            {
                //Add Member record
            }
            catch (Exception ex)
            {
                return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(ex.Message) };
            }
        }
    }

I think your error comes from the fact that the test project is .NET Core 3.1, while the project you are testing uses .NET Framework 4.5.1.我认为您的错误来自于测试项目是 .NET Core 3.1,而您正在测试的项目使用 .NET Framework 4.5.1。

Is there a reason you don't want to use the same runtime for the tests as for the project you are testing?您是否有理由不想为测试使用与您正在测试的项目相同的运行时?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从程序集“System.Web,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50 - Could not load type 'System.Web.UI.ParseChildrenAttribute' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50 无法从程序集“System.Web,版本=4.0.0.0,Cult=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Web.UI.ParseChildrenAttribute” - Could not load type 'System.Web.UI.ParseChildrenAttribute' from assembly 'System.Web, Version=4.0.0.0, Cult=neutral, PublicKeyToken=b03f5f7f11d50a3a' 无法从程序集&#39;System.Web,...&#39;加载类型&#39;...&#39; - Could not load type '…' from assembly 'System.Web, …' 无法加载文件或程序集“System.Web,Version = 4.0.0.0,Culture = neutral”或其依赖项之一。 访问被拒绝 - Could not load file or assembly 'System.Web, Version=4.0.0.0, Culture=neutral' or one of its dependencies. Access is denied .NETCore 3.1 - 错误 MC1000 未知构建错误,“找不到程序集”System.Web,版本 = 4.0.0.0 - .NETCore 3.1 - Error MC1000 Unknown build error, 'Could not find assembly 'System.Web, Version=4.0.0.0 无法加载文件或程序集 'System.Web.Services,版本 = 4.0.0.0 - Could not load file or assembly 'System.Web.Services, Version=4.0.0.0 无法从程序集 System.Web 加载类型“System.Web.HttpPostedFileBase” - Could not load type 'System.Web.HttpPostedFileBase' from assembly System.Web 无法加载文件或程序集&#39;System.ServiceModel.Web,版本= 4.0.0.0&#39; - Could not load file or assembly 'System.ServiceModel.Web, Version=4.0.0.0' 无法加载文件或程序集“ System.Web.MVC,版本4.0.0.0” - could not load file or assembly 'System.Web.MVC, version 4.0.0.0,' 出现错误-无法加载文件或程序集&#39;System.Web.Mvc,版本= 4.0.0.0 - getting error-could not load file or assembly 'System.Web.Mvc, Version=4.0.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM