Rhino Mocks 忽略私有方法

发布于 2025-01-03 15:57:32 字数 482 浏览 5 评论 0原文

我有一个类,其中包含一个名为 Validate 的方法,我想使用 Rhino Mocks 对其进行测试。 Validate 方法调用一个静态类,该类又访问 SQL 数据库。

我想告诉 rhino 模拟在执行测试时忽略对静态类的调用。 当调试下面的代码时,静态类仍然被调用并尝试访问数据库,而我想要它做的只是返回“错误消息文本”。

var mock = MockRepository.GenerateMock<DataUpdateTaskExecutor>();

string resourceName;
Expect.Call(SqlResourceHelper.GetString(resourceName)).IgnoreArguments().Return("error messaage text");

IList<string> errors;
Assert.AreEqual(false,mock.Validate(out errors));

I have a class with a method called Validate that I want to test using Rhino Mocks. The Validate method makes a call to a static class which in turn accesses an SQL database.

I want to tell rhino mocks to ignore the call to the static class when executing the test.
When debugging the code below the static class is still called and attempts to access the database when all I want it to do is return "error message text".

var mock = MockRepository.GenerateMock<DataUpdateTaskExecutor>();

string resourceName;
Expect.Call(SqlResourceHelper.GetString(resourceName)).IgnoreArguments().Return("error messaage text");

IList<string> errors;
Assert.AreEqual(false,mock.Validate(out errors));

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

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

发布评论

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

评论(2

暮年慕年 2025-01-10 15:57:32

Rhino.Mocks(以及大多数其他免费模拟框架,如 moq)无法模拟非虚拟成员。正如 @Joe Tuskan 所说,您应该将功能包装在接口中并将接口注入构造函数中,或者使其成为可以模拟的非静态方法。

如果您有钱可以花,可以使用商业模拟工具来模拟非虚拟成员。我对其中任何一个都没有任何经验,所以我不会提及任何一个的名字。

Rhino.Mocks (as well as most of the other free mocking frameworks like moq) can't mock non-virtual members. As @Joe Tuskan said, you should either wrap the functionality in an interface and inject the interface in the constructor or make it a non-static method that can be mocked.

If you've got money to spend, there are commercial mocking tools that allow non-virtual members to be mocked. I don't have any experience with any of them so I won't mention any by name.

夢归不見 2025-01-10 15:57:32

“如果你有钱花,有商业模拟工具可以让非虚拟成员被模拟。”,Telerik just mock 是一个很棒的模拟框架。这是值得付出的!

"If you've got money to spend, there are commercial mocking tools that allow non-virtual members to be mocked. ", Telerik just mock is an awesome mocking framework. it's worth paying!

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