How can I use Rhino mocks to mock just one value in a test object?
Been making some changes to our code, and now I need to re-factor some unit tests to make sure they're compatible with the new code. I've come up against a problem. In this:
ChannelLoad loader = new ChannelLoad(_customerDbMock, _bulkCopyMock);
loader.Execute(taskId);
The "loader" object is now trying to connect to another object in order to get a string value whe开发者_开发知识库reas before the string was returned automatically. So the obvious solution is to mock that object to return a suitable value for the test. However for various reasons I can't easily do this.
Ideally what I'd like to be able to do is to get a "real" (i.e as specified in code) loader object that performs a "real" Execute method but which has a "mock" value for that particular string. But I'm really not sure how to do this - even if it's possible - with Rhino Mocks. The string property in question isn't abstract or anything - it's protected and it's actually read-only. This is how it looks inside "loader":
protected string DbConnectionString
{
get
{
return _Service.GetLocalDatabase().GetConnectionString(_Service);
}
}
And the problem is that for the test environment "GetLocalDatabase" returns nothing.
Anyone help me out here? Is there a way I can mock this up using Rhino Mocks or is my only option to refactor the code to make it not rely on an external object? If the latter, pointers would also be helpful.
Cheers, Matt
Two other options if you don't want to inject _Service:
Create a class that inherits from ChannelLoad (call it "TestableChannelLoader" or something) and
overwrite the DbConnectionString-Property or
extract a method "GetConnectionString" that you can override in your new class and call it in the DbConnectionString-Property
I see that loader has dependency to _Service. So, you need to inject mock of this dependency to loader in order to change DbConnectionString property behavior.
精彩评论