模拟从另一个类继承的类

发布于 2025-01-02 22:19:41 字数 722 浏览 4 评论 0原文

所以这是这样的场景,我正在重构一些意大利面条代码。我的第一个问题是一系列类更新了其他类,我通过让我想要测试的类(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2025-01-09 22:19:41

DatabaseConnect 是否也有从中提取的接口?我认为您应该能够将其设置为:

public interface IDatabaseConnect

public class DatabaseConnect : IDatabaseConnect

public interface ISearchDatabaseConnect : IDatabaseConnect

public class SearchDatabaseConnect : DatabaseConnect, ISearchDatabaseConnect

现在制作 Mock 将从两个接口获取所有“东西”。


旁注,您的方法/构造函数可能应该采用接口,而不是具体的:

public Search(XmlAccess xmlFile, ISearchDatabaseConnect searchDatabaseConnection) { ... }

这样您就可以注入模拟,例如:

var mockedSearchDatabaseConnect = new Mock<ISearchDatabaseConnect>();
var search = new Search(xmlFile, mockedSearchDatabaseConnect.Object);

Does DatabaseConnect also have an interface extracted from it? I think you should be able to set it up like:

public interface IDatabaseConnect

public class DatabaseConnect : IDatabaseConnect

public interface ISearchDatabaseConnect : IDatabaseConnect

public class SearchDatabaseConnect : DatabaseConnect, ISearchDatabaseConnect

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:

public Search(XmlAccess xmlFile, ISearchDatabaseConnect searchDatabaseConnection) { ... }

That way you can inject the mock, like:

var mockedSearchDatabaseConnect = new Mock<ISearchDatabaseConnect>();
var search = new Search(xmlFile, mockedSearchDatabaseConnect.Object);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文