C # Rhino mocks stubmethod with hard-coded parameter in second call

It may not be something that is even possible, but I thought I would ask anyway. Is there anyway for me to complete this method so that the second call is also missed using the parameter provided in the method I tested?

Stub Method:

public SupportDetails GetSupportDetails(string languageKey) { var result = FindSupportDetails(languageKey); return result ?? FindSupportDetails("en-us"); } 

My Current test:

 public void GetsUSDetails_IfLangKeyDoesNotExist() { var langKey = "it-it"; _repo.Stub(s => s.FindSupportDetails(langKey)) .Return(supportDetails.Where(sd => sd.LanguageKey == langKey) .SingleOrDefault()); ISupportRepository repo = _repo; var actual = repo.GetSupportDetails(langKey); Assert.AreEqual("en-us", actual.LanguageKey); } 

and the supportDetails object used in the test:

 supportDetails = new SupportDetails[] { new SupportDetails() { ContactSupportDetailsID = 1, LanguageKey = "en-us" }, new SupportDetails() { ContactSupportDetailsID = 2, LanguageKey = "en-gb" }, new SupportDetails() { ContactSupportDetailsID = 3, LanguageKey = "es-es" } }; 
+5
source share
1 answer

The correct and most elegant solution to your problem is to use the Do method:

 _repo.Stub(s => s.FindSupportDetails(null)) .IgnoreArguments() .Do((Func<string, SupportDetails>) (langKey => supportDetails.SingleOrDefault(sd => sd.LanguageKey == langKey))); 

Func will raise regardless of which argument passed to FindSupportDetails , then the correct SupportDetails will be selected.

+4
source

All Articles