Ho un metodo chiamato GetTasks()
che restituisce 10 oggetti task. Voglio moqare questo compito per scopi di test unitari. Ecco il codice:Restituzione di diversi valori simulati in base ai parametri passati in Unit Test con Moq
_crateRecallService.Setup(m => m.GetTasks(It.IsAny<int>(), It.IsAny<List<Stage>>(), It.IsAny<List<Severity>>())).Returns(new List<CrateRecallTaskWithComms>()
{
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "1", PkCTaskID = 1, CampaignId = 1, Severity = "High"}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "2", PkCTaskID = 2, CampaignId = 2, Severity = "High"}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "3", PkCTaskID = 3, CampaignId = 3, Severity = "High"}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "4", PkCTaskID = 4, CampaignId = 4}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "5", PkCTaskID = 5, CampaignId = 5}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "6", PkCTaskID = 6, CampaignId = 6}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "7", PkCTaskID = 7, CampaignId = 7}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "8", PkCTaskID = 8, CampaignId = 8}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "9", PkCTaskID = 9, CampaignId = 9}},
new CrateRecallTaskWithComms() { CrateRecallTas = new CrateRecallTas() { CrateId = "10", PkCTaskID = 10, CampaignId = 10}}
});
Questo funziona bene, ma manca qualcosa. Il filtraggio sulla gravità delle attività non funzionerebbe qui.
La mia domanda è, come posso impostare Moq in modo che se l'elenco di Gravità passato ha un livello di gravità elevato, restituirà 3 invece di 10 attività? Quindi, in altre parole, se lo passo:
// Arrange
var severities = new List<Severity>() { Severity.High };
voglio tornare 3 compiti invece di 10.
Qualsiasi aiuto sarebbe molto apprezzato.
Non lo sapevo! – Liath
Si noti che l'uso di una lambda che restituisce un tipo ovvio, come '.Returns (arg => $" constantpart/{arg} ")' potrebbe richiedere l'aiuto lungo il sistema di inferenza del tipo con '.Returns (arg => $ "constantpart/{arg}") ' –
@TetsujinnoOni: o, come nella mia risposta, metti tipi espliciti nella lista degli argomenti della lambda, piuttosto che nella lista degli argomenti di tipo generico, come suggerisci. In altre parole, la mia risposta menziona già tipi espliciti nella firma. –