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”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值