Using ReturnsAsync When Automocking

1 Answer 36 Views
API Async Mocking JustMock Free Edition
Drew
Top achievements
Rank 1
Drew asked on 21 Dec 2023, 02:22 PM | edited on 21 Dec 2023, 02:23 PM

JustMock supports .ReturnsAsync() when arranging standard mocks, however I am unable to find a way to get this same behavior when Automocking with a MockingContainer<T>.

A solution I've found is to wrap the return value in Task.FromResult<T>(). This fulfills the expected Task<T> return type of the async function, however the syntax is a bit clunky.

service .Arrange<IRepository>(r => r.SomeAsyncMethod("input")) .Returns(Task.FromResult<ISomeModel>(resultModel));

Ideally, I would like to avoid building the Task<T> return values manually and just rely on .ReturnsAsync() to handle the conversion.

service .Arrange<IRepository>(r => r.SomeAsyncMethod("input")) .ReturnsAsync(resultModel);

It's possible the APIs just aren't there, but I want to ensure I'm not just missing some special syntax to get it working.

1 Answer, 1 is accepted

Sort by
1
Accepted
Tsvetko
Telerik team
answered on 03 Jan 2024, 10:18 AM

Hi Drew,

You are right.

JustMock is missing ReturnsAsync methods for automocking. The only way to mock async method for a container at the moment is to use regular Returns method and Task.FromResult.

I logged a feature request here.

Regards,
Tsvetko
Progress Telerik

A brand new JustMock course was just added to the Virtual Classroom - the free self-paced technical training portal that gets you up to speed with Telerik and Kendo UI products! Check it out at https://learn.telerik.com.
Tags
API Async Mocking JustMock Free Edition
Asked by
Drew
Top achievements
Rank 1
Answers by
Tsvetko
Telerik team
Share this question
or