Forgive me if this is a stupid question, but I'm fairly new at mocking, and am trying to get my head around it.
I have some unit tests (using the built-in Visual Studio 2010 Professional testing features), which use a stub that is needed by a method. I created a stub, and set the default return values for a couple of properties and a method, and all works well. I have a static class that sets up the stub, and this is used in the TestInitialize
method to set up the stub...
public static AppointmentReminderProviderInterface GetMockProvider() {
AppointmentReminderProviderInterface provider = MockRepository.GenerateStub<AppointmentReminderProviderInterface>();
provider.Stub(p => p.ContactName).Return(MockProviderContactName);
provider.Stub(p => p.ContactEmail).Return(MockProviderContactEmail);
return provider;
}
Note that MockProviderContactName
and MockProviderContactEmail
are local string properties that contain default data for the provider. The unit tsts that check to see if things work as expeced with the default data all pass fine.
However, I now want to test what happens when one of these properties contains duff data. I thought I could just set this on the stub, but it isn't working. The test method contains the following lines...
_provider.Stub(p => p.ContactEmail).Return("invalid");
Debug.WriteLine("Provider email: <" + _provider.ContactEmail + ">");
The Debug.WriteLine()
shows me that, despite the fact that I have set the ContactEmail
property to return "invalid" it still returns the default email address. This causes my test to fail, as I'm expecting it to throw an exception, and it doesn't.
Anyone any idea why I can't change the return value of this property?
Thanks for any help.