如何使用 Moq 测试 void 方法?

发布于 2025-01-06 17:12:02 字数 432 浏览 0 评论 0原文

我是 TDD 新手,我仍然有很多问题,其中一个问题是如何测试 void 方法!

我有这个参数通过引用传递的参数,我不知道如何测试:(这只是一个测试,不是我真正的方法)

public void ReplaceCenter(ref string cod, ref string name)
    {
        User user = userDAO.GetSomething();

        if (user.Cod != null)
        {
            cod = user.Cod;
        }

        if (user.Name != null)
        {
            name = user.Name;
        }
    }            

有人可以帮忙吗?谢谢!

Im new at TDD and i still have many questions, and one of this questions is how to test a void method!

I have this one with params passed by refference, and i have no clue how to test: (this is just a test, not my real method)

public void ReplaceCenter(ref string cod, ref string name)
    {
        User user = userDAO.GetSomething();

        if (user.Cod != null)
        {
            cod = user.Cod;
        }

        if (user.Name != null)
        {
            name = user.Name;
        }
    }            

Can someoen help? Thanks!

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

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

发布评论

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

评论(4

终陌 2025-01-13 17:12:02

假设 userDAO 作为依赖项注入并且可以被模拟,我将进行以下测试:

  1. 在模拟的 userDAO 上调用断言 GetSomething()
  2. 断言返回引用字符串 cod == 传入我们的 User.Cod
    用于创建模拟的 userDAO
  3. 断言返回的引用字符串 name == 传入 User.Cod 中
    我们曾经创建模拟的 userDAO

我同意避免 ref 参数,但我建议按照 @Tim Cools 的其他答案中的建议进行重构

Assuming userDAO was injected as a dependency and can be mocked I would do the following tests:

  1. Assert GetSomething() was called on mocked userDAO
  2. Assert returned reference string cod == passed in User.Cod that we
    used to create mocked userDAO
  3. Assert returned reference string name == passed in User.Cod that
    we used to create mocked userDAO

I agree with avoiding ref parameters though, I would suggest refactoring as suggested in the other answer by @Tim Cools

多彩岁月 2025-01-13 17:12:02

你应该尽量避免 ref 和 out参数。 使用包含这两个属性的对象。这将使你的设计更清晰,你的工作更清晰...

编辑:如果你真的想最小起订量输出参数使用,你可以找到一个例子 此处

you should try to avoid ref and out parameters. use an object that contains the two properties. this would make your design cleaner and your job much cleaner...

edit: if you really want to moq the out parameters use you can find an example here

巷子口的你 2025-01-13 17:12:02

只需在调用方法后测试参数中的值,模拟 userDAO 来控制它返回的内容(null、cod、name)

Just test the values in the params after you call the method, mocking the userDAO to control what it returns (null, cod, name)

云朵有点甜 2025-01-13 17:12:02

void 方法意味着将会有某种副作用。我通常建议如果可能的话避免使用它们,但是当您确实需要测试它们时,一般经验法则是:

  1. 断言对您的模拟进行了所有预期的调用。
  2. 如果可能,断言模拟是使用特定参数值调用的。这对于 void 方法更为重要,因为您正在测试副作用。
  3. 尽可能断言所有副作用,包括那些不应该改变的副作用。

实际上,这些断言也应该针对非 void 方法,也许更多的是为了防止意外的副作用,而不是测试预期的副作用。 (IMO,如果可能的话,函数应该具有零副作用。)

A void method implies that there is going to be some sort of side-effect. I generally suggest avoiding them, if possible, but when you do need to test one, general rules of thumb would be:

  1. Assert all expected calls on your mocks were made.
  2. If possible, assert that the mocks were called with specific parameter values. This is more important for void methods, because you are testing the side-effects.
  3. Assert every side-effect you can, including those that should not have changed.

Really, these assertions should be made for non-void methods, as well, perhaps more to prevent unexpected side-effects than to test for expected ones. (IMO, a function should have zero side-effects if at all possible.)

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