views:

42

answers:

0

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