简体   繁体   中英

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. I would like to introduce Unit testing using NUnit. I therefore added NUnit Test Project(.Net Core 3.1) to the solution. I have added a reference to the.Net Framework 4.5.1 in the NUnit project, after building it looks fine. 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

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.

Is there a reason you don't want to use the same runtime for the tests as for the project you are testing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM