J'ai lu quelques-unes des discussions sur le groupe d'utilisateurs Moq et n'ont pas réussi à trouver un exemple et ont été jusqu'à présent incapable de trouver le scénario que j'ai. Voici ma question et le code:moqueur comportement de collecte avec Moq
// 6 periods
var schedule = new List<PaymentPlanPeriod>()
{
new PaymentPlanPeriod(1000m, args.MinDate.ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(1).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(2).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(3).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(4).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(5).ToString())
};
// Now the proxy is correct with the schedule
helper.Setup(h => h.GetPlanPeriods(It.IsAny<String>(), schedule));
Puis dans mes tests, j'utiliser des périodes mais le Raillé _PaymentPlanHelper jamais Remplit la collection, voir ci-dessous pour l'utilisation:
public IEnumerable<PaymentPlanPeriod> Periods
{
get
{
if (CanCalculateExpression())
_PaymentPlanHelper.GetPlanPeriods(this.ToString(), _PaymentSchedule);
return _PaymentSchedule;
}
}
Maintenant, si je change l'objet moquaient utiliser une autre méthode surchargée de GetPlanPeriods qui renvoie une liste comme suit:
var schedule = new List<PaymentPlanPeriod>()
{
new PaymentPlanPeriod(1000m, args.MinDate.ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(1).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(2).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(3).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(4).ToString()),
new PaymentPlanPeriod(1000m, args.MinDate.Value.AddMonths(5).ToString())
};
helper.Setup(h => h.GetPlanPeriods(It.IsAny<String>())).Returns(new List<PaymentPlanPeriod>(schedule));
List<PaymentPlanPeriod> result = new _PaymentPlanHelper.GetPlanPeriods(this.ToString());
Cela fonctionne comme prévu. Tous les pointeurs serait génial, aussi longtemps que vous ne bash pas mon architecture ... :)
Merci, Stephen
Ai-je raison de dire que vous voulez que votre appel de méthode mockée modifie le paramètre de planification qui lui est passé? Cette question SO peut aider - la réponse utilise des rappels http://stackoverflow.com/questions/2183691/how-to-modify-an-invocation-parameter-of-a-mocked-method-with-moq –
Oui David. Je lis l'autre poste en ce moment. Je vous remercie. –