使用 MBUnit 的 Mirror 对私有 Func 委托方法进行单元测试
我想使用 MB 单位 Mirror.ForObject()
测试以下类 Private Func<>
委托方法。然而它并没有反映该方法。任何人都可以提供一种方法来做到这一点吗?
功能 代码类
public class AccountTransaction
{
private static readonly Func<decimal, decimal, decimal> NetAmountCalculator = (discountedValue, discountPecentage) => discountPecentage == 100 ? 0 : Math.Round(discountedValue / (1 - (discountPecentage / 100)), 2);
}
测试方法
/// <summary>
/// NetAmountCalculator in normal flow
/// </summary>
[Test]
public void NetAmountCalculatorTest()
{
var cashTransaction = Mirror.ForObject(new AccountTransaction());
decimal discountedAmount = 90;
decimal discountPecentage = 10;
cashTransaction["NetAmountCalculator"].Invoke(discountedAmount , discountPecentage);
Assert.IsTrue(true);
}
我已参考 MBUint help 以及另一个来自 google 的好帮助代码
I would like to test the following class Private Func<>
delegate method using MB Units Mirror.ForObject()
. However it is not reflecting the method. Could any please provide a way to do this?
Functionality Code class
public class AccountTransaction
{
private static readonly Func<decimal, decimal, decimal> NetAmountCalculator = (discountedValue, discountPecentage) => discountPecentage == 100 ? 0 : Math.Round(discountedValue / (1 - (discountPecentage / 100)), 2);
}
Test method
/// <summary>
/// NetAmountCalculator in normal flow
/// </summary>
[Test]
public void NetAmountCalculatorTest()
{
var cashTransaction = Mirror.ForObject(new AccountTransaction());
decimal discountedAmount = 90;
decimal discountPecentage = 10;
cashTransaction["NetAmountCalculator"].Invoke(discountedAmount , discountPecentage);
Assert.IsTrue(true);
}
I have referred MBUint help as well another nice help from google code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NetAmountCalculator
是您的类的一个字段。它不是方法或属性,因此您不能调用它(即使它实际上是一个委托,因此它看起来像一个方法)。您需要做的是获取该字段的值,并正确地对其进行转换,然后才能评估它返回的结果。NetAmountCalculator
is a field of your class. It's not a method or a property, and therefore you cannot invoke it (even if it's actually a delegate so it looks like a method). What you need to do is to get the value of the field, to cast it correctly, and only then you may evaluate the result it returns.