我已经在#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;
}
我的单元测试是:
[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&lt;&gt;该 存储库cardId变量,所以它显然失败了(结果来自于 第一种方法回归为零)。
我确定我错过了什么,但是......?
感谢您的帮助, 干杯, 特里
答案 0 :(得分:1)
从Moq 4.8开始(尚未发布,但有预发布版本),您可以使用ref
匹配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);
答案 1 :(得分:0)
我不太确定我是否理解了这个问题,但你在测试中已经为refCardId指定了0。
int? refCardId = 0;
你的模拟不会改变这个值,因为它不能。你的模拟只是一个代理,正是你在Setup方法中正在做的事情。在这种情况下,你的Mock在被调用时将返回1。它对refCardId没有任何作用。
请记住,您的sut是paymentRepository及其SaveCard方法 - 模拟仅用于测试sut而不依赖于dataContext。