单元测试遗留 C# 代码

发布于 2024-10-12 16:23:27 字数 407 浏览 1 评论 0原文

如何为这样的方法编写 NUnit 测试。这个方法本身是否值得重构? 处理遗留代码中的此类场景的最佳方法是什么?

         public bool DoXYZ()
            {
                ABC abc= new ABC()
                XYZ xyz = new XYZ();
                if (xyz .IsSomeCondition(Session.SessionID))
                { return false; }
                else
                { return abc.IsSomeOtherCondition(SessionID.SessionID); }
            }

How to write a NUnit test for a method like this. Does this method itself warrant refactoring?
What is the best approach to deal with scenarios like this in leagacy code?

         public bool DoXYZ()
            {
                ABC abc= new ABC()
                XYZ xyz = new XYZ();
                if (xyz .IsSomeCondition(Session.SessionID))
                { return false; }
                else
                { return abc.IsSomeOtherCondition(SessionID.SessionID); }
            }

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

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

发布评论

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

评论(4

寄意 2024-10-19 16:23:27

您可能需要重构它以引入用于依赖项注入的钩子。例如,包含 DoXYZ 方法的类可以获得 ABC 和 XYZ 的新属性。这些属性可以默认为 ABC 和 XYZ 的实例,但在单元测试中可以替换为模拟版本。

如果您更喜欢使用 IoC,这种方法也支持

You will probably need to refactor it to introduce hooks for dependency injection. For example, the class that contains the DoXYZ method can get new properties for ABC and XYZ. These properties could default to instances of ABC and XYZ, but in the unit tests could be replaced by mock versions.

And if you prefer to use IoC, this approach supports that as well

心作怪 2024-10-19 16:23:27

我肯定会重构以通过参数注入会话 ID - 否则您将必须手动创建会话。

可以做成静态的吗?看起来很像,特别是当您注入 sessionid 时。

另外,您正在实现一个(短)命令调度程序,与 IoC 相比,它通常被认为是一种反模式(请参见上面 Joel Martinez 的回答)。

I would definitely refactor to inject the session id via a parameter - otherwise you'll have to create the session manually.

Can it be made static? Looks like it, particularly if you inject sessionid.

Also, you're implementing a (short) command dispatcher, which is generally considered an anti-pattern, compared to IoC (see Joel Martinez' answer above).

梦忆晨望 2024-10-19 16:23:27

您有两种选择:

  • 重构代码并按照 Joel 和 Chris 的建议使用依赖项注入。这可能涉及大量工作,但它将使您的代码干净且易于测试。
  • 按照此问题中的要求使用高级框架来模拟非注入依赖项。

处理大型遗留代码的方法可能是同时使用这两种方法。

您还可以查看 MSDN 杂志中的这篇文章

You have two choices:

  • Refactor your code and use dependency injection as Joel and Chris suggested. This can involve a lot of work but it will make your code clean and easily testable.
  • Use advanced framework for mocking non injected dependencies as asked in this question.

The way to go in big legacy code is probably using both approaches.

You can also check this article from MSDN magazine.

她比我温柔 2024-10-19 16:23:27

考虑到问题中的混淆代码,我只能提供一些

  • 我看到的两条路径的指针 - 因此最少需要 2 个单元测试。
  • 在方法中创建协作者通常会很麻烦。将依赖项作为构造函数/方法参数传递。这允许您派生一个假值并操纵分支(例如,使 xyz.IsSomeCondition 对于此测试返回 false)。如果 ABC 和 XYZ 是易于设置的简单类,那么它可能不是一个迫在眉睫的问题..您现在可以不用提取参数重构。
  • 将 sessionId 提取到参数中以消除静态(全局)变量访问,这些访问通常会保留其先前的值,从而导致测试之间的依赖关系

public bool DoXYZ(ABC abc, XYZ xyz, Guid sessionId) 
{    if (xyz.IsSomeCondition(sessionId))    
        return false; 

     return abc.IsSomeOtherCondition(sessionId);  
}

Given the obfuscated code in the question, I can only offer a few pointers

  • I see 2 paths - so 2 unit tests needed minimum.
  • creating collaborators within a method is usually troublesome down the road. Pass in dependencies as ctor / method parameters. This allows you to derive a fake and manipulate branching (e.g. make xyz.IsSomeCondition return false for this test). If ABC and XYZ are simple classes that are easy to setup, then it may not be an immediate problem.. you could live without the Extract Parameter refactoring for now.
  • extract sessionId into a parameter to eliminate static (global) variable access, which normally retain their previous values causing dependencies between tests

.

public bool DoXYZ(ABC abc, XYZ xyz, Guid sessionId) 
{    if (xyz.IsSomeCondition(sessionId))    
        return false; 

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