问题描述
我的单元测试有问题.问题是:我正在使用 Sitecore.我问的另一个问题是:单元测试 Sitecore LicenseManager
I'm having problems with my unit tests. The problem is: I'm using Sitecore. Another question I asked about this is: Unit Testing Sitecore LicenseManager
我的问题是出现异常:
Test method foo.Bar.Framework.Tests.Core.UnitTest1.TestMethod1 threw exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Sitecore.Services.Heartbeat' threw an exception. ---> System.InvalidOperationException: Could not read Sitecore configuration.
我使用 dotPeek 查看了代码,Sitecorre.Configuration.Settings.GetSetting("HeartbeatInterval") 函数引发了异常.因为它不能读取配置.当我将相同的代码添加到我的单元测试 .cs 文件并调试测试时,它可以读取配置.该设置也可以在 app.config
I looked into the code with dotPeek and the function Sitecorre.Configuration.Settings.GetSetting("HeartbeatInterval") throws an exception. Because it can not read the configuration.When I add the same code to my unit test .cs file and debug the test it can read the configuration.The setting is also available in the app.config
看起来引用的程序集无法读取配置,而测试本身可以.我该如何解决这个问题?
It looks like the referenced assembly cannot read the config and the test itself can.How can I fix this?
编辑
Settings.GetSetting() 函数最终使用了 Sitecore.Configuration.ConfigReader.GetConfigNode().这个函数最终使用了这个函数:
The Settings.GetSetting() function eventually uses Sitecore.Configuration.ConfigReader.GetConfigNode(). This Function eventually uses this function:
private static ConfigReader GetReader()
{
return ConfigurationManager.GetSection("sitecore") as ConfigReader;
}
所以...为什么引用的dll不能通过ConfigurationManager读取app.config?
So...Why can't the referenced dll read the app.config through the ConfigurationManager?
推荐答案
原来是 *.testsettings.当我在我的电脑上运行它时,它可以工作,但在构建服务器上却没有.结果我只需要提交 *.testsettings 并包含/testsettings 参数
It turned out to be the *.testsettings.When I ran it on my pc it worked but on the build server it did not. Turned out I just had to commit the *.testsettings and include the /testsettings parameter
这篇关于引用的程序集无法读取 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!