Rhino模拟一个抽象类而不模拟其虚拟方法? [英] Rhino mock an abstract class w/o mocking its virtual method?
本文介绍了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模拟一个抽象类而不模拟其虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文