这就是通常与部分模拟完成,他们可以是一个有点恶心的。
首先,您嘲笑的方法必须是才是虚拟的。否则Rhino Mocks不能拦截该方法。所以让我们将您的代码更改为:
public Image Get(BrowserName browser)
{
// if no screenshot mode specified it means that regular screenshot needed
return this.Get(browser, ScreenshotMode.Regular);
}
public virtual Image Get(BrowserName browser, ScreenshotMode mode) {
// some code omitted here
}
请注意,第二种方法现在是虚拟的。然后,我们可以设置我们的部分模拟像这样:
//Arrange
var yourClass = MockRepository.GeneratePartialMock<YourClass>();
var bn = new BrowserName();
yourClass.Expect(m => m.Get(bn, ScreenshotMode.Regular));
//Act
yourClass.Get(bn);
//Assert
yourClass.VerifyAllExpectations();
,与AAA犀牛嘲笑语法的。如果您喜欢使用录制/回放,您也可以使用它。
所以这就是你将如何做到这一点。一个可能更好的解决办法是,如果ScreenshotMode
是一个枚举,你有C#4在您的处置,只让它作为一个可选的参数:
public Image Get(BrowserName browser, ScreenshotMode mode = ScreenshotMode.Regular)
{
//Omitted code.
}
现在,你不必有两种方法,所以没有必要测试一个叫另一个。除了制作方式虚拟(如vcsjones解释)
为什么不让它调用一个通用的私有方法? – 2012-08-09 15:57:49