nsubstitute / NSubstitute

A friendly substitute for .NET mocking libraries.
https://nsubstitute.github.io
Other
2.66k stars 261 forks source link

Support property getters with `When..Do` syntax #497

Open dtchepak opened 5 years ago

dtchepak commented 5 years ago

As raised in #449, it would be useful to support property getters in When..Do:

// Current syntax:
sub.When(x => { _ = x.Prop; }).DoNotCallBase(); 
// Proposed:
sub.When(x => x.MyProp).DoNotCallBase();

From @zvirja's comment:

    public static WhenCalled<TSubstitute> When<TSubstitute, TResult>(
            this TSubstitute substitute, Func<TSubstitute, TResult> substituteCall)
            where TSubstitute: class
    {
        return SubstituteExtensions.When(substitute, x => substituteCall(x));
    }

Need to ensure this does not conflict with the existing WhenCalled<T> When<T>(this T substitute, Func<T, Task> substituteCall) overload of this method.

304NotModified commented 5 months ago

Need to ensure this does not conflict with the existing WhenCalled<T> When<T>(this T substitute, Func<T, Task> substituteCall) overload of this method.

I think it does as Task is a class.

Another option is to remove the Func<T, Task> and do the type checking in this one.

Otherwise v6?