I suspect you could do this with Custom Matchers.
From moq's 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 a similar thing. Create a method that uses Match.Create<QueryRequest>
to match your key, e.g.
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 haven't tried this code, so forgive me if it breaks entirely.
Oh, and for some mildly related self-promo: exactly this kind of complexity is what bugs me about Moq and other mocking tools. This is why I created a mocking library that allows you to check for method arguments with normal code: http://github.com/eteeselink/FakeThat. It's in the middle of a major refactoring (and renaming) process though, so you might want to hold your breath. Nevertheless, I'd be thrilled to hear what you think of it.
EDIT: Oh, @nemesv beat me to it, with a (probably) better answer. Ah well.