-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support mocking the return value when the type is Task/Task<T> #171
Comments
This is already implemented.
Agreed. I'm looking at this now.
Somehow this sentence became a little garbled. I suppose you meant, "The type to mock must be an interface or an abstract or non-sealed class. If it is not able to be mocked, a System.ArgumentException will be thrown."? In order to stay true to how mock default values have been produced up until now, mocking a
|
This turned out to be quite difficult to implement, and this feature comes with a small runtime performance penalty, so some more optimization might be required later on. |
@JasonMing, @westonal, @kibiz0r, @fadookie, @jahmai, @almazik- I just published a pre-release version 4.8.0-rc1 on NuGet. It has (among other things) support for |
@stakx Was Moq already returning Task.FromResult(default(T)) or am I hallucinating? |
@jahmai - You're only partially hallucinating. 😉 public abstract class Inner { }
public abstract class Outer
{
public abstract Task<Inner> GetInnerAsync();
}
var mock = new Mock<Outer> { DefaultValue = DefaultValue.Mock };
var inner = await mock.Object.GetInnerAsync();
Assert.NotNull(inner); // NEW: This assertion used to fail, but now it passes.
// `inner` will not be `null`, but a mocked instance of `Inner`. This makes it possible to directly |
When
Mock<T>.DefaultValue == DefaultValue.Empty
, the default return value of async method, which returnsTask
orTask<T>
, will directly return null, thenawait
on them will cause NullRefereceException.In this case, the "Empty" value should be
Task.FromResult(default(T))
.When
Mock<T>.DefaultValue == DefaultValue.Mock
the default return value of async method will change to the mockingTask
/Task<T>
, however, theTask
/Task<T>
are not able to be mocked, then the System.ArgumentExceptionType to mock must be an interface or an abstract or non-sealed class. will be thrown.In this case, the "Mock" value should be
Task.FromResult(Mock.Of<T>())
.The text was updated successfully, but these errors were encountered: