C#:System.Configuration.Configuration在寻找什么?
我有以下测试方法:C#:System.Configuration.Configuration在寻找什么?,c#,C#,我有以下测试方法: [TestMethod] public void TestHarvestMethod() { HarvestTargetTimeRangeUTC time = new HarvestTargetTimeRangeUTC(); time.StartTimeUTC = new DateTime(2008, 01, 01, 00, 00, 00, DateTimeKind.Utc); time.EndTimeUTC = DateTime.UtcNow;
[TestMethod]
public void TestHarvestMethod()
{
HarvestTargetTimeRangeUTC time = new HarvestTargetTimeRangeUTC();
time.StartTimeUTC = new DateTime(2008, 01, 01, 00, 00, 00, DateTimeKind.Utc);
time.EndTimeUTC = DateTime.UtcNow;
XElement lIntelexReport = XElement.Parse(rawXml);
Harvester target = new Harvester();
target.ConfigureHarvester((System.Configuration.Configuration)null);
var res = target.Harvest(time);
Console.WriteLine(res);
}
与此方法配合使用的:
public void ConfigureHarvester(System.Configuration.Configuration configuration)
{
reportId = Int32.Parse(configuration.AppSettings.Settings["IncidentReport"].Value);
}
public XElement Harvest(HarvestTargetTimeRangeUTC ranges)
{
XElement lIntelexReport = IntelexServiceCall();
return XMLConversion(QueryData(ranges, lIntelexReport));
}
要测试此方法,请执行以下操作:
public void ConfigureHarvester(System.Configuration.Configuration configuration)
{
reportId = Int32.Parse(configuration.AppSettings.Settings["IncidentReport"].Value);
}
public XElement Harvest(HarvestTargetTimeRangeUTC ranges)
{
XElement lIntelexReport = IntelexServiceCall();
return XMLConversion(QueryData(ranges, lIntelexReport));
}
问题是,我收到了Null异常错误,指出“对象引用未设置为对象的实例”。在这一行:
reportId = Int32.Parse(configuration.AppSettings.Settings["IncidentReport"].Value);
我几乎是肯定的,这是由这里的空值引起的:
target.ConfigureHarvester((System.Configuration.Configuration)null);
上述行中的系统配置是本车间常用的配置,但通常用于以下方法:
public void ConfigureHarvester(System.Configuration.Configuration configuration)
{
context = new PlannedOutageFactorDataContext();
}
所以我的reportid字段显然在寻找一个空值以外的值,问题是我不知道它到底在寻找什么。我读了MSDN的System.Configuration,但它确实没有帮助。如果有人能给我指出正确的方向,我将不胜感激 它希望您将web.config或app.config值的副本传递给它,以便它可以从中提取所需的信息(保存在AppSettings部分) 比如说
<configuration>
<appSettings>
<add key="IncidentReport" value="1" />
</appSettings>
</configuration>
在测试程序集中,您需要有一个app.config文件。在此文件中,您需要一个名为“IncidentReport”的密钥使用整数值。您正在向函数传递一个
null
。您期望得到什么?最后一个代码示例甚至没有使用传入的配置
参数。因为它没有被使用,所以不管它是什么。System.Configuration.Configuration在寻找什么?答案是:它正在寻找一个配置文件。将app.config添加到测试项目中,并使用IncidentReport添加appsettings部分value@Oded我知道我正在传递一个null,我知道它需要更改。我不知道它在寻找什么样的数据。@Esen谢谢。这正是我要寻找的方向。+1将关键字名称更改为IncidentReport
,这样它就与问题更相关了。还有一个int值,正如Robb建议的那样,你应该得到勾号:)@johnmithell谢谢你的帮助。我现在明白了。这是我第一次对System.Configuration做任何事情,尽管我读了MSDN,但它只是让我更加困惑。