使用 MBUnit 的 Mirror 对私有 Func 委托方法进行单元测试

发布于 2024-11-30 04:26:27 字数 1208 浏览 1 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(1

我早已燃尽 2024-12-07 04:26:27

NetAmountCalculator 是您的类的一个字段。它不是方法属性,因此您不能调用它(即使它实际上是一个委托,因此它看起来像一个方法)。您需要做的是获取该字段的,并正确地对其进行转换,然后才能评估它返回的结果。

var cashTransaction = Mirror.ForObject(new AccountTransaction());
decimal discountedAmount = 90;
decimal discountPecentage = 10;
object fieldValue = cashTransaction["NetAmountCalculator"].Value;
var func = (Func<decimal, decimal, decimal)fieldValue;
decimal actualResult = func(discountedAmount , discountPecentage);

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.

var cashTransaction = Mirror.ForObject(new AccountTransaction());
decimal discountedAmount = 90;
decimal discountPecentage = 10;
object fieldValue = cashTransaction["NetAmountCalculator"].Value;
var func = (Func<decimal, decimal, decimal)fieldValue;
decimal actualResult = func(discountedAmount , discountPecentage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文