模拟从另一个类继承的类
所以这是这样的场景,我正在重构一些意大利面条代码。我的第一个问题是一系列类更新了其他类,我通过让我想要测试的类(Search.cs)的构造函数将其需要的类作为依赖项来解决这个问题,现在看起来像这样。
public Search(XmlAccess xmlFile, SearchDatabaseConnect searchDatabaseConnection)
{
this.xmlFile = xmlFile;
FsdcConnection = searchDatabaseConnection;
Clear();
}
我正在将其更新到链条的更上游。这一切都很好,但我有一个小问题。
我注入的类继承自另一个类,我有 Resharper 并且提取了接口,但问题是依赖类继承自另一个具体类 - 明白我的意思吗?
public class SearchDatabaseConnect : DatabaseConnect, ISearchDatabaseConnect
{ // }
我不知道如何处理DatabaseConnect上的继承?我该如何嘲笑呢?显然,如果不存在的话,我就可以模拟 ISearchDatabaseConnect 了,然后就可以走了,但我仍停留在具体类的继承上。我确信在我的谷歌搜索失败之前人们就已经遇到过这个问题,因为我找不到这方面的例子。
预先感谢您提供任何有用的建议。
So here is the scenario, I am refactoring some spaghetti code. My first problem was a chain of classes that newed up other classes, I fixed this by making the ctor of the class I want to test (Search.cs) take the class it needs as a dependency, it looks like this now.
public Search(XmlAccess xmlFile, SearchDatabaseConnect searchDatabaseConnection)
{
this.xmlFile = xmlFile;
FsdcConnection = searchDatabaseConnection;
Clear();
}
I'm newing it up further up the chain. That is all good but I have a little problem.
The class that I am ctor injecting inherits from another class, I have Resharper and I have extracted interfaces but the problem is the dependency class inherits from another concrete class - see what I mean?
public class SearchDatabaseConnect : DatabaseConnect, ISearchDatabaseConnect
{ // }
I don't know what to do about the inheritance on DatabaseConnect? How do I mock that? Obviously if that wasn't there I would be all set I could mock an ISearchDatabaseConnect and away we go but I am stuck on the inheritance of a concrete class. I am sure people have run into this before my googl'ing was a failure when it came to finding examples about this.
Thanks in advance for any useful suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DatabaseConnect
是否也有从中提取的接口?我认为您应该能够将其设置为:现在制作
Mock
将从两个接口获取所有“东西”。旁注,您的方法/构造函数可能应该采用接口,而不是具体的:
这样您就可以注入模拟,例如:
Does
DatabaseConnect
also have an interface extracted from it? I think you should be able to set it up like:And now making a
Mock<ISearchDatabaseConnect>
will get all the "stuff" from both interfaces.Side-note, your method/constructor there should probably take in the interface, not the concrete:
That way you can inject the mock, like: