xunit “could not load type” error

谁都会走 提交于 2021-01-28 19:44:17

问题


I inherited a project and the tests ran fine. Today, I created a new class, and xunit errors out when I try to use this class in a test.

Both the test project and the app are targeting .NET Framework 4.6.1.

I do clean and rebuild on the both the solution and the test project, but I still get the error.

error:

Could not load type 'MyNewClass' from assembly 'MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

project.json:

"buildOptions": {
  "warningsAsErrors": true
},
"testRunner": "xunit",
"dependencies": {
  "xunit": "2.2.0-beta4-build3404",
  "dotnet-test-xunit": "2.2.0-preview2-build1029",
  "MyApp": "1.0.0-*",
  "Microsoft.EntityFrameworkCore.InMemory": "1.0.0",
  "NSubstitute": "1.9.2"
},

"frameworks": {
    "net461": {
    }
}

Anyone have any ideas how to fix this?


回答1:


Sometimes for whatever reason .NET Core dependencies restore won't work as expected. I've just tried your project.json on my machine and it was complaining about xunit version, however after several project.json edits it was able to restore all the dependencies, and run tests successfully.

In such situations following might help:

  1. Try dotnet restore and/or dotnet build commands in the folder where your project.json file is.
  2. Delete project.json.lock file and try step #1.
  3. Use dotnet test to run unit tests.

I find these commands to run .NET Core projects more stable and provide better error output for troubleshooting than Visual Studio.




回答2:


So, my problem was that MyApp.dll was not getting updated/re-created, even after I did a clean and rebuild. I had to physically delete the files (including MyApp.dll) in MyApp\test\MyApp.Tests\bin\Debug\net461\win7-x86




回答3:


I had this problem too and could solve it recreating the project.

Sometimes, things simply stops working correctly, as pointed in the others answers, and the best is just start over, restart the program, recreate the project, reboot the machine...

This is Visual Studio, sigh... :)



来源:https://stackoverflow.com/questions/40293669/xunit-could-not-load-type-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!