RhinoMocks 使用属性的默认实现

发布于 2024-10-08 04:53:56 字数 516 浏览 0 评论 0原文

我有一些与实体框架一起使用的代码,例如

class Person{
  pubic Person() {
     Address = new Address();
  }
  public virtual Address Address { get; set; }
}

我将 Address 标记为 virtual 的原因是为了延迟加载。

现在为了测试,我正在对 Person 进行存根处理。但由于它是存根的,Address getter 只是返回 null (即使它是在构造函数中设置的)。如果我存根 Address 属性 (person.Stub(x => x.Address).Return(new Address());) ,一切正常。但我真的不想把财产毁掉!有没有办法告诉RhinoMocks不要重写这个getter,即使它是虚拟的?

I have some code that I use with Entity Framework like

class Person{
  pubic Person() {
     Address = new Address();
  }
  public virtual Address Address { get; set; }
}

The reason I'm marking Address as virtual is for lazy loading.

Now to test, I'm stubbing the Person. But since it's stubbed, the Address getter just returns null (even though it's set in the constructor). If I stub out the Address property (person.Stub(x => x.Address).Return(new Address());) things work fine. But I don't really want to have to stub out the property! Is there any way to tell RhinoMocks not to override this getter even though it's virtual?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-10-15 04:53:56

当然可以,但是你必须使用部分模拟:

var person = MockRepository.GeneratePartialMock<Person>();

Sure, but you have to use a partial mock:

var person = MockRepository.GeneratePartialMock<Person>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文