I suspect you can do this with custom matches.
From moq QuickStart Page :
// custom matchers mock.Setup(foo => foo.Submit(IsLarge())).Throws<ArgumentException>(); ... public string IsLarge() { return Match.Create<string>(s => !String.IsNullOrEmpty(s) && s.Length > 100); }
I suspect you could do that. Create a method that uses Match.Create<QueryRequest> to match your key, for example
public QueryRequest CorrectKey(string key) { return Match.Create<QueryRequest>(qr => qr.Key == key); }
and then
_query.Setup(q => q.Execute(CorrectKey(key))).Returns(new QueryResponse {Customer = customer});
Note. I have not tried this code, so forgive me if it completely breaks.
Oh, and for some of the slightly related ones, itβs just that kind of complexity thatβs what bothers me about Moq and other mocking tools. That's why I created a ridiculous library that allows you to check the arguments of a method with regular code: http://github.com/eteeselink/FakeThat . This is in the middle of the main refactoring (and renaming) process, although you might want to hold your breath. However, I would be delighted that you think about it.
EDIT: Oh, @nemesv beat me up, with a (possibly) better answer. Good.
source share