Capture matcher value at runtime for .Returns

2 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    1 posts
    Member since:
    Jul 2013

    Posted 10 Jul 2013 Link to this post

    Hi there.  I'm trying to figure out if there's a way to reference the runtime value of a matcher for use in a Returns call.  This seems like a pretty straightforward question, but I wasn't able to find quite what I'm wanting in the documentation, so apologies if I missed something obvious.  Regardless, here's the call I'm trying to make.

    service.Arrange<Employee>(emp => emp.LoadByName(Arg.AnyString)).Returns(data.employees.Where(e => e.Name == /* whatever the value matched by Arg.AnyString was */));

    Is there another method I should be calling to accomplish the same thing, or is there simply a way to reference the matcher's value?
  2. Stefan
    Stefan avatar
    198 posts

    Posted 11 Jul 2013 Link to this post

    Hello Jonathan,

    Just pass a delegate to Returns that has the same number and type of arguments like the arranged method. Like so:

    service.Arrange<Employee>(emp => emp.LoadByName(Arg.AnyString))
        .Returns((string name) => data.employees.Where(e => e.Name == name));

    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top