How can I write unit test to the following code:
public Image Get(BrowserName browser)
{
// if no screenshot mode specified it means that regular screenshot needed
return this.Get(browser, ScreenshotMode.Regular);
}
public Image Get(BrowserName browser, ScreenshotMode mode) {
// some code omitted here
}
That's typically done with a partial mock, and they can be a little yucky.
First, the method you are mocking must be virtual. Otherwise Rhino Mocks can't intercept the method. So let's change your code to this:
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
}
Note that the second method is virtual now. We can then setup our partial mock like so:
//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();
That's with the AAA Rhino Mocks syntax. If you prefer to use record / playback, you can use that too.
So that's how you would do it. A possibly better solution is if ScreenshotMode
is an enum and you have C# 4 at your disposal, just make it an optional parameter:
public Image Get(BrowserName browser, ScreenshotMode mode = ScreenshotMode.Regular)
{
//Omitted code.
}
Now you don't have two methods, so there is no need to test that one calls another.