我目前正试图让我的一个单元测试工作,但有一件事阻碍了。我有一个名为 AccountingScheduleLookup 的类,它附加了一个只读的 ID 字段。当我尝试模拟对使用此 ID 字段的方法的调用时,它会在该特定代码行上向我抛出一个可爱的 NullReferenceException。这意味着 ID 没有分配给它,或者对象没有被实例化。
但是,当我确实进行了基本实例化时,它仍然抛出异常。关于如何解决这个问题的任何想法?
这是一个代码示例(值得)
AccountingScheduleLookup = new AccountingSchedule { Description = "Will this work?" }
var calendarPeriods = dal.GetObjects<AccountingScheduleDetail>(
Where.Property("AccountingScheduleID").Is(AccountingScheduleLookup.AccountingScheduleID));
最佳答案
rhino mocks 的典型用法包括设置一个预期,即当该属性被访问时它会返回一个特定的值。
这里是最新版本的 rhino mocks 的快速引用:
http://www.ayende.com/wiki/GetFile.aspx?File=Rhino+Mocks+3.3+Quick+Reference.pdf
以下是可能适合您的部分:
属性 getter
Expect.Call(foo.Name).Return("Bob");
关于c# - 帮我避免这个 NullReferenceException(使用 Rhino Mocks),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3266017/