I have read a lot about mocking, particularly using Rhino Mocks and have learned that Rhino Mocks can only mock interface methods which are virtual or virtual methods in concrete classes. I have read that the reason for this is because Rhino mocks can't intercept non-virtual methods and this is where I am stuck. What does it mean by intercepting methods? How does intercepting methods actually work with regards to mocking (Rhino Mocks in particular)
Basically the idea is that it creates a 'behind the scenes' class that overrides any virtual or interface methods and plugs in 'mock' code into them.
if you have (based on your comment question)
public EmailHelper
{
public virtual int SendEmail( MailMessage message)
{
var server = ConnectToServer();
int statusCode = server.SubmitEmail( message );
return statusCode;
}
}
and then in a test (I forget Rhino's syntax for this, but this is close enough)
var mock = Mocks.CreateMockFor<EmailHelper>();
Expect.Call(mock.SendEmail).Return(5);
behind the scenes it will use reflection to load up the SomeClass Type
object, search it for interface implementations and virtual methods and generate a class something like
public MockEmailHelper
{
public override int SendEmail( MailMessage message )
{
return 5;
}
}
So as you can see, when you call the mock version of SendEmail, it wont connect to server etc, it will just do what you told it to, so you can test your code that depends on the 'email module' without actually sending email.