You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2019单元测试项目引用主项目后无法访问类求助

Troubleshooting Unit Test Reference Issues in Visual Studio 2019

Hey there! I totally get how frustrating it is when you’ve checked all the obvious boxes—public modifiers, adding the right using statements, setting up the project reference—but still can’t access your main project’s classes in your unit tests. Let’s walk through some common fixes that might resolve this, especially since you’re following an older course (version mismatches are super common here!):

  • Verify Target Framework Compatibility
    This is the #1 culprit when references seem broken but look correct. If your main project and unit test project target different .NET frameworks (e.g., main project uses .NET Framework 4.7.2, test project uses .NET Core 3.1), they won’t play nicely together.
    To check: Right-click each project → Properties → Go to the Application tab → Confirm the "Target framework" dropdown shows the same version for both projects. If not, adjust the test project to match the main project’s framework.

  • Double-Check Your Test Project Template
    Older courses might use legacy test project templates that don’t align with modern project types. If your main project is a .NET Core/.NET 5+ project, make sure your test project is using a compatible template like MSTest Test Project (.NET Core) or xUnit Test Project (.NET Core)—not the old .NET Framework-only MSTest template. If you picked the wrong one, it’s easier to create a new test project with the correct template and re-add your tests than to fix the existing one.

  • Clean and Rebuild the Entire Solution
    Visual Studio’s build cache can sometimes cause weird reference glitches. Here’s how to clear it:

    1. Go to the top menu → Build → Clean Solution
    2. Then go to Build → Rebuild Solution
    3. For good measure, you can manually delete the bin and obj folders from both your main project and test project directories, then rebuild again.
  • Confirm the Reference is a Project Reference (Not a DLL Reference)
    Sometimes when adding references, you might accidentally pick a compiled DLL instead of the project itself. In your test project’s References folder, look for your main project’s name—if it shows a DLL path instead of the project name, remove that reference and re-add it by right-clicking the test project → Add → Project Reference → Select your main project from the list.

  • Double-Check Namespaces
    Even if you added using projectName;, make sure the class you’re trying to access is actually in that namespace. Right-click the class in your main project → Copy Full Name (this includes the full namespace), then paste that into your test code to reference the class directly (e.g., projectName.SubNamespace.MyClass). If that works, adjust your using statement to include the correct sub-namespace.

  • Update Visual Studio 2019
    Older versions of VS2019 have known bugs with project references and framework compatibility. Go to Help → Check for Updates to install the latest patches and updates for VS2019—this often resolves hidden version-related issues.

Give these steps a try in order—most of the time, the framework mismatch or build cache is the problem. Let me know if any of these fix it, or if you need to dig deeper!

内容的提问来源于stack exchange,提问作者Darren Nienaber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:50:55