removing dependancy of a private function inside a public function using Rhino Mocks
I am new to mocking, and have started with Rhino Mocks. My scenario is like this..in my class library i have a public function and inside it i have a private function call, which gets output from a service.I want to remove the private function dependency.
public class Employee
{
public virtual string GetFullName(string firstName, string lastName)
{
string middleName = GetMiddleName();
return string.Format("{0} {2} {1}", firstName, lastName,middleName );
}
private virtual string GetMiddleName()
{
// Some call to Service
return "George";
}
}
This is not my real scenario though, i just wanted to know how to remove depen开发者_如何学Godency of GetMiddleName() function and i need to return some default value while unit testing.
Note : I won't be able to change the private function here..or include Interface..Keeping the functions as such, is there any way to mock this.Thank
The problem is this part: "a private function call, which gets output from a service". That service should be injected, so that you can mock it out. If it creates a concrete instance of the service itself, then I don't think Rhino can help you.
TypeMock may be able to help you - I believe that lets you mock out anything, but at the cost of being more invasive.
You shouldn't remove the dependency of calling the private method, you should remove the dependency within that private method. If you can't do that, your code simply isn't testable without something like TypeMock.
One possible solution is to use the Extract and Override pattern to derive a testable class from the class under test and override the private
method (which should be something other than private
in your example as a private
method cannot be virtual
- perhaps you meant protected
?) to allow you to override the method.
public class Employee
{
public virtual string GetFullName(string firstName, string lastName)
{
string middleName = GetMiddleName();
return string.Format("{0} {2} {1}", firstName, lastName,middleName );
}
protected virtual string GetMiddleName()
{
// Some call to Service
return "George";
}
}
///<summary>
///Testable Employee class
///</summary>
public class TestableEmployee : Employee
{
public string MiddleName;
public virtual string GetFullName(string firstName, string lastName)
{
string middleName = GetMiddleName();
return string.Format("{0} {2} {1}", firstName, lastName,middleName );
}
protected override string GetMiddleName()
{
// provide own implementation to return
// property that we can set in the test method
return MiddleName;
}
}
test method
[TestMethod]
public GetFullName_ReturnsSetName()
{
var testEmployee = new TestableEmployee();
string firstName = "first";
string middleName = "middle";
string lastName = "last";
TestableEmployee.MiddleName = middleName;
string fullName = GetFullName(firstName, lastName);
Assert.AreEqual(string.Format("{0} {2} {1}",
firstName, lastName, middleName ),
fullName
);
}
if GetMiddleName()
is wrapper around a service call, then it might be a more testable design to have the service interface as a property of the Employee
class. This way, you can mock out the service type using the Extract and Override pattern or using an Inversion of Control (IoC) container like Unity or Castle Windsor.
精彩评论