Mocking ref help

20 views
Skip to first unread message

Terry_Brown

unread,
Sep 3, 2010, 6:57:22 AM9/3/10
to Moq Discussions
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

Daniel Cazzulino

unread,
Sep 3, 2010, 7:36:26 AM9/3/10
to moq...@googlegroups.com
make the mock strict and see if there's another call match being made.

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471



Terry_Brown

unread,
Sep 3, 2010, 8:25:29 AM9/3/10
to Moq Discussions
That's going to mean everything within the interface needs to have
a .Setup against it before I can unit test? Not ideal, but if that's
the only solution, I'll have to start mapping the rest of the
interface.

At present, that's the only part of that interface that I've .Setup
and unite tested against, so would have preferred to resolve without
going strict unless absolutely necessary?

Cheers,
Terry

Daniel Cazzulino

unread,
Sep 3, 2010, 8:46:07 AM9/3/10
to moq...@googlegroups.com
going strict tells you exactly what's matched. from there you can infer which setups are wrong, fix them, and go back to loose.


/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471



Cheers,
Terry

Reply all
Reply to author
Forward
0 new messages