You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When defining the setup of a method call, I am able to access the invocation arguments when defining the return value, as shown in the sample from the QuickStart guide:
// access invocation arguments when returning a value
mock.Setup(x => x.DoSomethingStringy(It.IsAny<string>())).Returns((strings)=> s.ToLower());
However, I don't seem to be able to do the same when setting up a method with Throws or ThrowsAsync, for example something like:
// unable to access invocation arguments when throwing a value (this is will not build)
mock.Setup(x => x.DoSomethingStringy(It.IsAny<string>())).Throws((strings)=>new Exception($"Tried to DoSomethingStringy with {s.ToLower()}"));// or, a case that seems particularly useful is setting properties on custom exception
mock.Setup(x => x.FindByIdAsync(It.IsAny<int>())).ThrowsAsync((intid)=>new IdNotFoundException(id,"Can't find the given id"));
I can work around this by using Callback to throw an exception instead:
mock.Setup(x => x.DoSomethingStringy(It.IsAny<string>())).Callback((strings)=>thrownew Exception($"Tried to DoSomethingStringy with {s.ToLower()}"));
but it would be much clearer and more consistent if I could use the Throws/ThrowsAsync syntax instead
The text was updated successfully, but these errors were encountered:
Support for argument access has been added to Throws in #1191 (thanks again, @adam-knights!).
Regarding ThrowsAsync, let's wait with adding support there for now. That method might very well be marked obsolete (see https://github.com/moq/moq/issues/53#issuecomment-755221874), so there is no point in adding dozens of method overloads as long as we're not sure whether they'll stay around long-term.
When defining the setup of a method call, I am able to access the invocation arguments when defining the return value, as shown in the sample from the QuickStart guide:
However, I don't seem to be able to do the same when setting up a method with Throws or ThrowsAsync, for example something like:
I can work around this by using Callback to throw an exception instead:
but it would be much clearer and more consistent if I could use the Throws/ThrowsAsync syntax instead
The text was updated successfully, but these errors were encountered: