c# - 帮我避免这个 NullReferenceException(使用 Rhino Mocks)

标签 c# nunit rhino-mocks rhino nunit-console

我目前正试图让我的一个单元测试工作,但有一件事阻碍了。我有一个名为 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/

相关文章:

c# - 增量运算符如何与数组一起使用?

c# - "There were not enough free threads in the ThreadPool to complete the operation."

c# - 无法在 Xamarin C# 中将图像设置为 UIButton

c# - 多次记录(或运行)单元测试

c# - 如何测试在被测类中调用的方法?

C# Rhino 在第二次调用中使用硬编码参数模拟 stubmethod

c# - 如何将 azure 排队消息发送到 azure sql 数据库

c# - 如何重试 NUnit 测试用例?

c# - RhinoMock - AssertWasCalled 在具有不同参数的相同方法上不起作用

c# - 使用 Rhino Mocks 从模拟/ stub 中引发事件