单元测试抛出NullReferenceException

菲多夫兰兹

我一直在尝试对从xml文件加载数据并将其作为列表返回的方法进行测试,但是,在运行时,我一直收到NullReferenceException异常。我已经检查过了,我的文件路径也很好。

[TestCase(1)]
public void firstTest(int num)
{
    var studentList = StudentListGenerator.CreateStudentList(@"../../TestReport/TestData.xml");

    Assert.IsTrue(studentList.Count() > num);
}

堆栈跟踪指向我的CreateStudentList方法中的一行代码,如果我将其直接插入测试本身,则该代码行得很好(当我正常运行该方法时,该方法总体上运行良好)。

String xData = File.ReadAllText(filePath);
var x = new XmlSerializer(typeof (Report));
Report dataConverted = (Report) x.Deserialize(new StringReader(xData)); 
// the last line is where the stack trace ends

有人对我要去哪里出错了吗?

编辑:这是一些StudentListGenerator的链接,其中包含CreateStudentList方法:https : //gist.github.com/jekrch/eecdd1c8de8a11268be0

这是完整的堆栈跟踪:

在PFdata.Dashboard.Data.StudentListGenerator.CreateStudentList(String filePath)中位于C:\ Users \ CodeCamp \ Desktop \ StudentDataDashboard \ StudentDataDashboard \ Dashboard.Data \ StudentListGenerator.cs:DashboardTests.StudentDataCalcTestTests.firstTest(Int32) :\ Users \ CodeCamp \ Desktop \ StudentDataDashboard \ DashboardTests \ StudentDataCalcTests.cs:line 22结果消息:System.NullReferenceException:对象引用未设置为对象的实例。

菲多夫兰兹

因此,关于原始错误的stacktrace具有误导性。我正在测试的CreateStudentList方法中NullReferenceException的真实来源是以下行:

Application.Current.Properties["reportMonth"] = reportMonth;

问题在于测试未实例化Application类。因此解决方案是像这样在测试开始时简单地实例化应用程序,

var app = new Application();

我有一个想法,可以从Ben Raupov关于类似问题评论中尝试非常感谢Unicorn2帮助我提出了许多其他可能性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章