我正在尝试根据行为驱动开发进行编程,
它指出,不写任何代码都不应写
首先失败的单元测试。
我的问题:
有没有比以下更好的解决方案:
当我编写使用这些私有(private)方法的公共(public)方法时;
或
属性。
最佳答案
我已经与它斗争了1个多月,但找到了答案:
var objectOfPrivateMethod = new ObjectOfPrivateMethod(); //yes here is contructor
object[] arguments = { }; // here as Object you provide arguments
var extractedPrivateMethod = typeof(ObjectOfPrivateMethod).GetMethod("Name_Of_Private_Method", BindingFlags.NonPublic|BindingFlags.Static); //if fails returns null. delete flag static if it's not static. Returns your method as an object.
Assert.AreNotEqual(null, extractedPrivateMethod, "Mathod does not exist"); // good to catch if even exists.
object result = extractedPrivateMethod.Invoke(null, arguments); // here as object you'll get return value of your function. change null for object of class where is method, if your method is not static
就这样。
关于unit-testing - 如何在 BDD/TDD 中对私有(private)方法进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1583363/