Moq return what was passed in
Copy link. StoreAsync It. StoreAsync vr ; Assert. DoSomething " ping ". Returns true ;. Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment. Concat a,b,c ; You always need to reference all the arguments, to match the method's signature, even if you're only going to use one of them.
Steve Steve 3, 1 1 gold badge 16 16 silver badges 27 27 bronze badges. This should be the accepted answer. This is exactly what you need to do. Anything else throws a "number of expected arguments" exception.
Yea, definitely allot easier to read and works with ReturnsAsync too! This answer saved the day. Note future readers , you can take it a bit further as well. Personally, this is a much better answer. I have very little knowledge of Moq but still I understood it immediately.
For methods returning void I used. Message ; — tymtam. Niclas Lindqvist 1, 2 2 gold badges 15 15 silver badges 24 24 bronze badges. WDuffy WDuffy 7, 5 5 gold badges 35 35 silver badges 42 42 bronze badges. In a test, a mocked version of an IParser can be created by Moq as the following initial test demonstrates:. The preceding test will fail however because the mocked TryParse has not been configured correctly, for example specifying that the method should return true.
In the following test the Callback method is used to set the ref value. To be able to do this, a delegate must first be defined that matches the signature of the mocked method that contains the ref parameter.
The text was updated successfully, but these errors were encountered:. Sorry, something went wrong. I know about It. IsAny already but that requires specifying per parameter. I was wondering if there is any possibility of a feature that allows similar functionality without relying on specifying each parameter via It.
I don't think there's a lot of value in that, especially since it will break badly if you add overloads of Add that have a different return value. And you lose the ability to refactor your APIs since now those "magic strings" will get out of date. That would only give you the name of one of the overloads. If you happen to rename the others, you won't notice. This would really help for mocking Microsoft methods because they love to put in option params with a default of null.