[英]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.