NUnit Error: The Type Initializer for … Threw an Exception (And app.config)

My Visual Studio solution contains 5 projects, one of them is for unit testing purpose.

The unit testing project is referencing to Entity Framework library where I query data from database. Well, since the Entity Framework is depending on app.config for the connection string, my NUnit returns error when I run it. This is because NUnit can’t find the connection string in app.config file. The error is:

System.TypeInitializationException: The type initializer for ‘…’ threw an exception.
System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Here’s the screenshot (some information is blacked out on purpose). I use NUnit 2.5.9 for unit testing.

 

 

 

Two workarounds for this problem are stated below. You don’t have to apply both workarounds, just either one of them. In my case, the first one works like charm.

  1. Copy app.config file, paste it in the same folder as your .nunit file and rename it to the same as your NUnit project filename.
    Example: NUnit project file is “C:/Users/Documents/Nunit/ProjectTest.nunit”. My app.config will have to be “C:/Users/Documents/Nunit/ProjectTest.config”
  2. Copy app.config file, paste it in the same folder as your library (.dll) file and rename it to the same as your library (.dll) filename.
    Example: Library project file is “C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.dll”. My app.config will have to be “C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.dll.config” (if doesn’t work, try rename it to ”C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.config”)

<think> 我们在处理C#错误CS0246,具体是关于NUnit命名空间或类型找不到的问题。根据错误信息,通常是因为缺少using指令或程序集引用。 解决步骤: 1. 确保已安装NUnit框架。可以通过NuGet包管理器安装NUnit包。 2. 在代码文件中添加using指令:`using NUnit.Framework;` 3. 如果安装后仍然报错,检查项目文件(.csproj)中是否引用了正确的NUnit程序集。 具体操作: - 在Visual Studio中,右键点击项目 -> 管理NuGet程序包 -> 搜索NUnit并安装。 - 或者使用包管理器控制台:`Install-Package NUnit -Version 3.12.0`(版本号可根据需要调整) 如果已经安装但仍有问题,尝试: - 清理并重新生成解决方案。 - 检查项目目标框架版本是否与NUnit版本兼容。 另外,如果使用NUnit测试框架,通常还需要安装NUnit3TestAdapter,以便在测试资源管理器中运行测试。 因此,完整的步骤可能是: 1. 安装NUnit包。 2. 安装NUnit3TestAdapter包(可选,用于测试运行)。 3. 在测试类中添加`using NUnit.Framework;` 4. 重新生成项目。 如果问题依旧,可以尝试删除项目的`bin`和`obj`文件夹,然后重新生成。 注意:如果项目是使用.NET Core,确保安装的NUnit包支持.NET Core。 根据引用[^1]的提示,我们也可以查看错误列表,并通过复制错误信息来搜索解决方案,但根据错误代码和描述,上述方法是标准的解决流程。 现在,生成相关问题: §§相关问题§§ 1. 如何安装和使用NuGet包管理器? 2. 在C#中,如何清理和重新生成解决方案? 3. 如何检查.NET项目的目标框架版本? 4. 为什么需要NUnit3TestAdapter? 5. 除了CS0246,还有哪些常见的C#编译错误? </think> ### 解决C#错误CS0246方法(NUnit缺失问题) #### 错误原因分析 错误代码 `CS0246` 表示编译器无法识别 `NUnit` 命名空间,通常由以下原因导致: 1. **缺少程序集引用**:项目未添加NUnit框架的DLL引用 2. **未添加using指令**:代码文件顶部缺少 `using NUnit.Framework;` 3. **NuGet包未安装**:未通过NuGet安装NUnit包[^1] 4. **项目配置错误**:目标框架版本不兼容或生成配置问题 #### 完整解决步骤 1. **安装NUnit NuGet包** 在Visual Studio中: - 右键点击项目 → "管理NuGet程序包" - 搜索 "NUnit" → 安装最新稳定版(推荐`NUnit`和`NUnit3TestAdapter`) ```powershell # 或使用包管理器控制台 Install-Package NUnit -Version 3.13.3 Install-Package NUnit3TestAdapter ``` 2. **添加using指令** 在测试类文件顶部添加: ```csharp using NUnit.Framework; // 必需的命名空间声明 ``` 3. **检查程序集引用** - 右键项目 → "添加" → "引用" - 在"程序集"选项卡中勾选 `nunit.framework.dll` - 确认 `.csproj` 文件包含引用: ```xml <ItemGroup> <PackageReference Include="NUnit" Version="3.13.3" /> </ItemGroup> ``` 4. **清理并重建解决方案** - 菜单栏 → "生成" → "清理解决方案" - 再次执行"重新生成解决方案" 5. **验证目标框架兼容性** - 右键项目 → "属性" → "应用程序"选项卡 - 确保目标框架与NUnit版本匹配(如.NET Core 3.1+或.NET 5+) #### 验证修复 创建示例测试类验证: ```csharp using NUnit.Framework; namespace MyTests; public class SampleTests { [Test] // 此特性来自NUnit public void TestAddition() { Assert.AreEqual(4, 2 + 2); // 使用NUnit的断言 } } ``` #### 常见问题排查 - 若仍报错,删除 `bin` 和 `obj` 文件夹后重新生成 - 检查NuGet包源配置(工具 → 选项 → NuGet包管理器) - 确保未误装 `NUnitLite`(简化版不包含完整框架)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值