CodexBloom - Programming Q&A Platform

Visual Studio 2022 - working with 'Could not load file or assembly' scenarios during unit testing with Moq and NUnit

👀 Views: 23 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-13
visual-studio unit-testing nunit moq .net6 C#

Does anyone know how to I'm following best practices but I'm working on a personal project and I've looked through the documentation and I'm still confused about I've been banging my head against this for hours. I'm working with a frustrating scenario when trying to run my unit tests in Visual Studio 2022. After updating to .NET 6, I'm getting the following behavior when executing tests: ``` Could not load file or assembly 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system want to find the file specified. ``` This occurs specifically when I run tests that utilize Moq for mocking and NUnit as the test framework. My test project references the main project where the assembly resides, but it seems to be unable to locate it during runtime. I've confirmed that the project builds successfully without errors, and the assembly is present in the output directory. I've tried cleaning and rebuilding the solution, and I've also deleted the `bin` and `obj` folders before rebuilding, but nothing seems to work. Additionally, I've checked the references in the test project and they all appear to be correct. Here's a snippet of the test code that fails: ```csharp [Test] public void MyService_ShouldReturnExpectedResult_WhenCalled() { var mockRepo = new Mock<IMyRepository>(); mockRepo.Setup(repo => repo.GetData()).Returns(new List<MyData>()); var service = new MyService(mockRepo.Object); var result = service.GetData(); Assert.IsNotNull(result); } ``` The test runs fine if I comment out the usage of the mocked repository, which suggests that the scenario may be related to the Moq assembly. I've ensured that I have the latest version of Moq installed via NuGet. Could this be a question related to assembly binding redirects or a configuration scenario? Any insights would be greatly appreciated! For context: I'm using C# on Ubuntu. How would you solve this? I'm working with C# in a Docker container on Windows 10. Thanks for taking the time to read this! Has anyone else encountered this? Am I missing something obvious? Thanks for any help you can provide!