如何在BDD/TDD中对私有方法进行单元测试? [英] How to unit test private methods in BDD / TDD?

查看:103
本文介绍了如何在BDD/TDD中对私有方法进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据行为驱动开发进行编程, 指出没有编写任何代码行都不得编写 首先单元测试失败.

I am trying to program according to Behavior Driven Development, which states that no line of code should be written without writing failing unit test first.

我的问题是,如何将BDD与私有方法一起使用?
如何对私有方法进行单元测试?
有没有比以下更好的解决方案:
-首先公开私有方法,然后将其私有 当我编写使用这些私有方法的公共方法时;

-在C#中将所有私有方法设为内部并使用InternalsVisibleTo 属性.

My question is, how to use BDD with private methods?
How can I unit test private methods?
Is there better solution than:
- making private methods public first and then making them private when I write public method that uses those private methods;
or
- in C# making all private methods internal and using InternalsVisibleTo attribute.

推荐答案

在编写代码"test-first"时,您是针对公共接口编写的.此时没有私有方法.

When you write code test-first, you write against the public interface. There are no private methods at this point.

然后编写代码以通过测试.如果这些代码中的任何一个被分解为私有方法,那都不重要-它应该仍然存在,仅是因为它已被公共接口使用.

Then you write the code to pass the test. If any of that code gets factored into a private method, that's not important -- it should still be there only because it is used by the public interface.

如果未先编写代码测试,则-在.net中,无论如何-反射可用于直接生产私有方法;尽管这是不得已的技术.

If the code isn't written test first, then -- in .net, anyway -- reflection can be used to directly prod private methods; though this is a technique of last resort.

这篇关于如何在BDD/TDD中对私有方法进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆