devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.87k stars 800 forks source link

Request: Ability to setup a property of type Task<ConcreteType> by mocking interface #1460

Open wizofaus opened 7 months ago

wizofaus commented 7 months ago

Category

Describe the feature

I'm trying to mock a class in a library that uses concrete types for several of its properties, but I happen to know that the properties are only ever accessed via an interface that the concrete type implements. This works fine:

        `mock.SetupGet<IInterface>(l => l.Property).Returns(Mock.Of<IInterface>())`

Despite the fact that the mocked type defines "Property" as an object of type "ConcreteType" (presumably it would fail if there were code that relied on Property genuinely being of type "ConcreteType", but so far no such issues).

The issue is that one property is actually of type Task<ConcreteType>, and I want to replace it with mocked Task<IInterface>, and so far I haven't found any syntax that allows that.

If there were a SetupAsync( ) or similar function (matching ReturnsAsync()), I would think it might be possible?

Back this issue Back this issue

github-actions[bot] commented 2 weeks ago

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

kzu commented 1 week ago

You should be able to set the return value value to Task.FromResult(Mock.OfType<IInterface>()), I think.