Rhino模拟一个抽象类而不模拟其虚拟方法?

编程入门 行业动态 更新时间:2024-10-27 08:25:30
本文介绍了Rhino模拟一个抽象类而不模拟其虚拟方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以执行驻留在使用Rhino Mocks模拟的抽象类上的虚拟方法的主体吗?

Can I execute the body of a virtual method that lives on an abstract class which has been mocked using Rhino Mocks?

要明确的是,我没有尝试模拟虚拟方法的行为。我正在尝试/ test /虚拟方法(在模拟类上)。

To be clear, I'm not trying to mock the behavior of the virtual method. I'm trying to /test/ the virtual method (on the mocked class).

这个想法是否公然滥用了Rhino Mocks?

Is this idea a blatant misuse of Rhino Mocks?

推荐答案

是的,那绝对没问题。我不能说我已经尝试过了,但是如果失败了,我会感到非常惊讶。

Yes, that should be absolutely fine. I can't say I've tried it, but I'd be very surprised if it failed.

编辑:我怀疑您想要 PartialMock 方法。例如:

I suspect you want the PartialMock method. Here's an example:

using System; using Rhino.Mocks; public abstract class Abstract { public virtual int Foo() { return Bar() * 2; } public abstract int Bar(); } class Test { static void Main(string[] args) { MockRepository repository = new MockRepository(); Abstract mock = repository.PartialMock<Abstract>(); using (repository.Record()) { Expect.Call(mock.Bar()).Return(5); } Console.WriteLine(mock.Foo()); // Prints 10 } }

编辑:或者是我第一次尝试在AAA:

Or in my first attempt at AAA:

using System; using Rhino.Mocks; public abstract class Abstract { public virtual int Foo() { return Bar() * 2; } public abstract int Bar(); } class Test { static void Main(string[] args) { // Arrange Abstract mock = MockRepository.GeneratePartialMock<Abstract>(); mock.Stub(action => action.Bar()).Return(5); // Act int result = mock.Foo(); // Assert mock.AssertWasCalled(x => x.Bar()); // And assert that result is 10... } }

更多推荐

Rhino模拟一个抽象类而不模拟其虚拟方法?

本文发布于:2023-10-31 05:42:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   方法   抽象类   Rhino

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!