使用Rhino Mocks来模拟私有对象调用

发布于 2024-12-11 01:32:57 字数 932 浏览 0 评论 0原文

所以我只是习惯了嘲笑东西。我在这里有这个私有变量:

private CoreDataContext _coreDataManager;

在这个类上:

public class RatesControlReport
        : Chatham.Panda.Batch.ProcessDefinition.BatchProcess

这个类有一个 void 方法,我想测试它,称为 CheckSwaptionVols(DateTime runDate)

在测试的第一部分中,我可以实例化主类:

RatesControlReport ratesControlReportProcess;
            ratesControlReportProcess = new RatesControlReport();

基本上我想进行此调用:

ratesControlReportProcess.CheckSwaptionVols(DateTime.Now);

但是此方法使用私有变量,如下所示:

System.Data.Linq.ISingleResult<CheckSwaptionVols> swaptionStatusResult = _coreDataManager.CheckSwaptionVols(this._runDate);

我希望能够传递此变量的模拟版本并返回我自己指定的 System.Data.Linq.ISingleResult ,以便测试可以继续而不依赖于数据库。

我该怎么做?

So I'm just getting used to mocking stuff. I have this private variable here:

private CoreDataContext _coreDataManager;

On this class:

public class RatesControlReport
        : Chatham.Panda.Batch.ProcessDefinition.BatchProcess

This class has a void method on it that I want to test called CheckSwaptionVols(DateTime runDate).

In the first part of my test I can instantiate the main class:

RatesControlReport ratesControlReportProcess;
            ratesControlReportProcess = new RatesControlReport();

And basically I want to make this call:

ratesControlReportProcess.CheckSwaptionVols(DateTime.Now);

However this method uses the private variable like so:

System.Data.Linq.ISingleResult<CheckSwaptionVols> swaptionStatusResult = _coreDataManager.CheckSwaptionVols(this._runDate);

I'd love to be able to pass in a mocked version of this variable instead and return my own specified System.Data.Linq.ISingleResult<CheckSwaptionVols> so the test can continue without a dependency on the DB.

How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

○闲身 2024-12-18 01:32:57

嗯,这取决于您实例化 CoreDataContext 的位置。如果这是在静态上下文或构造函数中构造的,则实际上无法为其创建模拟。这就是为什么在对象内部实例化依赖项通常被认为是不好的做法。您需要做的是提供一些依赖注入的方法。这可以像重载构造函数一样简单:

public RatesControlReport(CoreDataContext context)
{
    _coreDataManager = context;
}

...或者如果您绝望的话甚至可以是内部方法:

internal void InjectContext(CoreDataContext context)
{
    _coreDataManager = context;
}

但是,一般来说,在构造您的代码时始终提供 CodeDataContext 对象被认为是最佳实践费率控制报告。这会将数据访问与业务逻辑分开,从而使您能够更有效地对这两个类进行单元测试。

Well, it depends on where you instantiate your CoreDataContext. If this is constructed in a static context, or in the constructor, there's really no way to create a mock for it. This is why it is generally considered bad practice to instantiate dependencies inside the object. What you need to do is provide some method of dependency injection. This can be as simple as an overloaded constructor:

public RatesControlReport(CoreDataContext context)
{
    _coreDataManager = context;
}

...or even an internal method if you're desperate:

internal void InjectContext(CoreDataContext context)
{
    _coreDataManager = context;
}

Generally speaking, however, it is considered best practice to always provide your CodeDataContext object when constructing your RatesControlReport. This will separate the data access from the business logic, which will allow you to unit test both classes more effectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文