使用 Rhino Mocks 删除公共函数内私有函数的依赖
我是嘲笑的新手,并从 Rhino Mocks 开始。我的场景是这样的..在我的类库中,我有一个公共函数,在其中我有一个私有函数调用,它从服务获取输出。我想删除私有函数依赖项。
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";
}
}
但这不是我的真实场景,我只是想知道如何删除 GetMiddleName() 函数的依赖关系,并且我需要在单元测试时返回一些默认值。
注意:我无法在这里更改私有函数..或包含接口..保留函数本身,有什么方法可以模拟它。谢谢
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 dependency 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是这部分:“私有函数调用,从服务获取输出”。应该注入该服务,以便您可以模拟它。如果它创建服务本身的具体实例,那么我认为 Rhino 无法帮助您。
TypeMock 也许可以帮助你 - 我相信它可以让你模拟任何东西 ,但代价是更具侵入性。
您不应该删除调用私有方法的依赖关系,而应该删除该私有方法内部的依赖关系。如果你做不到这一点,那么如果没有像 TypeMock 这样的东西,你的代码根本无法测试。
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.
一种可能的解决方案是使用提取和覆盖模式从被测类派生一个可测试类,并覆盖
private
方法(该方法应该是您的测试中的private
以外的方法)例如,私有
方法不能是虚拟
- 也许您的意思是受保护
?)以允许您覆盖该方法。。
如果
GetMiddleName()
是服务调用的包装器,那么将服务接口作为Employee
类的属性可能是一个更可测试的设计 这样,您可以使用提取和覆盖模式或使用控制反转 (IoC) 容器(例如 Unity 或 温莎城堡。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 thanprivate
in your example as aprivate
method cannot bevirtual
- perhaps you meantprotected
?) to allow you to override the method.test method
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 theEmployee
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.