模拟 ref 参数

发布于 2024-09-17 21:42:54 字数 5047 浏览 3 评论 0原文

我已将其交叉发布到 #moq 讨论组: http://groups.google.com/group/moqdisc/browse_thread/thread/ 569b75fd2cc1829d

嘿伙计们,

我确信我遇到了一个模拟引用参数的问题 一定是显而易见的,但作为框架的新手,我无法使用它 出去。

我有以下存储库方法:

public int SaveCard(int userId, CardPaymentMethodDto
cardPaymentMethodDto)
{
        int? cardId = 0;
        try
        {
                int result = this.dataContext.usp_PaymentMethod_Card_Insert(userId,
                                cardPaymentMethodDto.UserAccountReference,
                                cardPaymentMethodDto.EncryptedCardNumber,
                                cardPaymentMethodDto.BinRange,
                                cardPaymentMethodDto.LastFourDigits,
                                cardPaymentMethodDto.ExpiryMonth,
                                cardPaymentMethodDto.ExpiryYear,
                                cardPaymentMethodDto.IssueNumber,
                                cardPaymentMethodDto.IssuingBank,
                                cardPaymentMethodDto.IssuingCountry,
                                cardPaymentMethodDto.Scheme,
                                cardPaymentMethodDto.StartMonth,
                                cardPaymentMethodDto.StartYear,
                                cardPaymentMethodDto.BillingAddress.House,
                                cardPaymentMethodDto.BillingAddress.SubPremises,
                                cardPaymentMethodDto.BillingAddress.Street,
                                cardPaymentMethodDto.BillingAddress.Town,
                                cardPaymentMethodDto.BillingAddress.County,
                                cardPaymentMethodDto.BillingAddress.Postcode,
                                cardPaymentMethodDto.BillingAddress.Country,
                                cardPaymentMethodDto.DateRegistered,
                                ref cardId);

                if (result <= 0)
                {
                        CompanySystemSqlException tex = new
CompanySystemSqlException("Database communications error");
                        tex.Data.Add("UserId", userId);
                        tex.Data.Add("PaymentMethod", cardPaymentMethodDto.ToString());
                        Logger.LogException("SaveCard: result <= 0", tex);
                        throw tex;
                }
        }
        catch (DbException ex)
        {
                CompanySystemSqlException tex = new
CompanySystemSqlException("Database communications error", ex);
                tex.Data.Add("UserId", userId);
                tex.Data.Add("PaymentMethod", cardPaymentMethodDto.ToString());
                Logger.LogException("SaveCard: DbException", tex);
                throw tex;
        }

        return cardId ?? 0;

}

a 我的单元测试是:

[Test]
public void SaveCard_ValidData_ShouldReturnValidCardId()
{
        int cardId;
        int? refCardId = 0;
        dataContext.DefaultValue = DefaultValue.Mock;
        dataContext.Setup( x =>
x.usp_PaymentMethod_Card_Insert(It.IsAny<int>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<DateTime>(), ref refCardId)).Returns(1);

        cardId = paymentRepository.SaveCard(1, new CardPaymentMethodDto
{ BillingAddress = new AddressDto { Country = "", County = "", House =
"", Postcode = "", Street = "", SubPremises = "", Town = ""},
                                                                                                        BinRange = "", Cv2 = 123,
                                                                                                        DateRegistered = DateTime.Now, EncryptedCardNumber =
"jumble",
                                                                                                        ExpiryMonth = "02", ExpiryYear = "2030", Id = 1,
IssueNumber = 1, IssuingBank = "", IssuingCountry = "",
                                                                                                        LastFourDigits = "", LastUsed = DateTime.MinValue, Scheme
= "", StartMonth = "", StartYear = "",
                                                                                                        UserAccountReference = ""});

        Assert.That(cardId, Is.EqualTo(1));

}

现在显然我必须在单元中创建 ref param (refCardId) 测试,否则无法设置预期结果,但 refCardId <>这 存储库 cardId 变量,所以它显然失败了(结果来自 第一种方法返回为零)。

我确信我错过了一些东西,但是什么......?

感谢您的帮助, 干杯, 特里

I've cross posted this on the #moq discussion group at:
http://groups.google.com/group/moqdisc/browse_thread/thread/569b75fd2cc1829d

hey folks,

I have come across a problem with a mocked ref param that I'm sure
must be obvious, but being new to the framework I just can't work it
out.

I have the following repository method:

public int SaveCard(int userId, CardPaymentMethodDto
cardPaymentMethodDto)
{
        int? cardId = 0;
        try
        {
                int result = this.dataContext.usp_PaymentMethod_Card_Insert(userId,
                                cardPaymentMethodDto.UserAccountReference,
                                cardPaymentMethodDto.EncryptedCardNumber,
                                cardPaymentMethodDto.BinRange,
                                cardPaymentMethodDto.LastFourDigits,
                                cardPaymentMethodDto.ExpiryMonth,
                                cardPaymentMethodDto.ExpiryYear,
                                cardPaymentMethodDto.IssueNumber,
                                cardPaymentMethodDto.IssuingBank,
                                cardPaymentMethodDto.IssuingCountry,
                                cardPaymentMethodDto.Scheme,
                                cardPaymentMethodDto.StartMonth,
                                cardPaymentMethodDto.StartYear,
                                cardPaymentMethodDto.BillingAddress.House,
                                cardPaymentMethodDto.BillingAddress.SubPremises,
                                cardPaymentMethodDto.BillingAddress.Street,
                                cardPaymentMethodDto.BillingAddress.Town,
                                cardPaymentMethodDto.BillingAddress.County,
                                cardPaymentMethodDto.BillingAddress.Postcode,
                                cardPaymentMethodDto.BillingAddress.Country,
                                cardPaymentMethodDto.DateRegistered,
                                ref cardId);

                if (result <= 0)
                {
                        CompanySystemSqlException tex = new
CompanySystemSqlException("Database communications error");
                        tex.Data.Add("UserId", userId);
                        tex.Data.Add("PaymentMethod", cardPaymentMethodDto.ToString());
                        Logger.LogException("SaveCard: result <= 0", tex);
                        throw tex;
                }
        }
        catch (DbException ex)
        {
                CompanySystemSqlException tex = new
CompanySystemSqlException("Database communications error", ex);
                tex.Data.Add("UserId", userId);
                tex.Data.Add("PaymentMethod", cardPaymentMethodDto.ToString());
                Logger.LogException("SaveCard: DbException", tex);
                throw tex;
        }

        return cardId ?? 0;

}

a the unit test I have is:

[Test]
public void SaveCard_ValidData_ShouldReturnValidCardId()
{
        int cardId;
        int? refCardId = 0;
        dataContext.DefaultValue = DefaultValue.Mock;
        dataContext.Setup( x =>
x.usp_PaymentMethod_Card_Insert(It.IsAny<int>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>(), It.IsAny<DateTime>(), ref refCardId)).Returns(1);

        cardId = paymentRepository.SaveCard(1, new CardPaymentMethodDto
{ BillingAddress = new AddressDto { Country = "", County = "", House =
"", Postcode = "", Street = "", SubPremises = "", Town = ""},
                                                                                                        BinRange = "", Cv2 = 123,
                                                                                                        DateRegistered = DateTime.Now, EncryptedCardNumber =
"jumble",
                                                                                                        ExpiryMonth = "02", ExpiryYear = "2030", Id = 1,
IssueNumber = 1, IssuingBank = "", IssuingCountry = "",
                                                                                                        LastFourDigits = "", LastUsed = DateTime.MinValue, Scheme
= "", StartMonth = "", StartYear = "",
                                                                                                        UserAccountReference = ""});

        Assert.That(cardId, Is.EqualTo(1));

}

Now obviously I've had to create the ref param (refCardId) in the unit
test, or I can't setup the expected result, but refCardId <> the
repository cardId variable, so it's obviously failing (result from the
first method is coming back as zero).

I'm sure I'm missing something, but what...?

Thanks for any help,
Cheers,
Terry

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

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

发布评论

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

评论(2

梦冥 2024-09-24 21:42:54

从 Moq 4.8(尚未发布,但有预发布版本)开始,您可以使用 It.Ref.IsAny 匹配 ref 参数:

dataContext.Setup(x => x.usp_PaymentMethod_Card_Insert(
                           It.IsAny<int>(),
                           It.IsAny<string>(),
                           ...
                           It.IsAny<DateTime>(),
                           ref It.Ref<int?>.IsAny))
           .Returns(1);

Starting with Moq 4.8 (which isn't released yet, but there's a pre-release version), you can match ref parameters using It.Ref<T>.IsAny:

dataContext.Setup(x => x.usp_PaymentMethod_Card_Insert(
                           It.IsAny<int>(),
                           It.IsAny<string>(),
                           ...
                           It.IsAny<DateTime>(),
                           ref It.Ref<int?>.IsAny))
           .Returns(1);
避讳 2024-09-24 21:42:54

我不太确定我是否理解这个问题,但您在测试中已将 0 分配给 refCardId 。

int? refCardId = 0;

你的模拟不会改变这个值,因为它不能。您的模拟只是一个代理,它完全按照您在安装方法中告诉它的方式执行。在这种情况下,你的 Mock 在被调用时将返回 1。它不会对 refCardId 执行任何操作。

请记住,您的 sut 是 paymentRepository 及其 SaveCard 方法 - 模拟仅用于测试 sut,而不依赖于 dataContext。

I am not quite sure if i understood the problem but you have assigned 0 to refCardId in your test.

int? refCardId = 0;

Your mock will not change this value because it can't. Your mock is only a proxy that is exactly doing what you are telling it in its Setup method. In this case your Mock will return 1 when it is called. it does not do anything with the refCardId.

Keep in mind that your sut is paymentRepository and its SaveCard method - the mock is only there to test the sut without the dependency on the dataContext.

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