From 924ef981d3fec6b2b402c7a38894ce71e98b0415 Mon Sep 17 00:00:00 2001 From: Maria Chervenkova Date: Mon, 7 Oct 2024 17:50:36 +0300 Subject: [PATCH 1/3] fix: Implement methods for automocking #759 - Implement Arrange and ReturnsAsync methods for Task; - Implement Arrange method for ValueTask; --- Telerik.JustMock/AutoMock/MockingContainer.cs | 78 +++++++++++++++---- .../Expectations/AsyncExtensions.cs | 44 +++++++++-- 2 files changed, 100 insertions(+), 22 deletions(-) diff --git a/Telerik.JustMock/AutoMock/MockingContainer.cs b/Telerik.JustMock/AutoMock/MockingContainer.cs index 595f250b..9aba960d 100644 --- a/Telerik.JustMock/AutoMock/MockingContainer.cs +++ b/Telerik.JustMock/AutoMock/MockingContainer.cs @@ -19,6 +19,7 @@ limitations under the License. using System.Linq; using System.Linq.Expressions; using System.Reflection; +using System.Threading.Tasks; using Telerik.JustMock.AutoMock.Ninject; using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings.Resolvers; using Telerik.JustMock.AutoMock.Ninject.Syntax; @@ -183,32 +184,75 @@ public FuncExpectation Arrange(Expression this.Get().Arrange(expression)); } - /// + /// + /// Entry-point for setting expectations. + /// + /// Mocking interface + /// Target expression + /// + /// Reference to to setup the mock. + /// + public FuncExpectation Arrange(Expression> expression) + { + return ProfilerInterceptor.GuardInternal(() => this.Get().Arrange(expression)); + } + + /// /// Entry-point for setting expectations. /// - /// - /// Mocking interface - /// + /// Mocking interface + /// Task result type /// Target expression /// - /// Reference to to setup the mock. + /// Reference to to setup the mock. /// - public ActionExpectation Arrange(Expression> expression) + public FuncExpectation> Arrange(Expression>> expression) + { + return ProfilerInterceptor.GuardInternal(() => this.Get().Arrange(expression)); + } + +#if NETCORE + /// + /// Entry-point for setting expectations. + /// + /// Mocking interface + /// Task result type + /// Target expression + /// + /// Reference to to setup the mock. + /// + public FuncExpectation> Arrange(Expression>> expression) + { + return ProfilerInterceptor.GuardInternal(() => this.Get().Arrange(expression)); + } +#endif + + /// + /// Entry-point for setting expectations. + /// + /// + /// Mocking interface + /// + /// Target expression + /// + /// Reference to to setup the mock. + /// + public ActionExpectation Arrange(Expression> expression) { return ProfilerInterceptor.GuardInternal(() => this.Get().Arrange(expression)); } - /// - /// Entry-point for setting expectations. - /// - /// - /// Mocking interface - /// - /// Target action - /// - /// Reference to to setup the mock. - /// - public ActionExpectation ArrangeSet(Action action) + /// + /// Entry-point for setting expectations. + /// + /// + /// Mocking interface + /// + /// Target action + /// + /// Reference to to setup the mock. + /// + public ActionExpectation ArrangeSet(Action action) { return ProfilerInterceptor.GuardInternal(() => this.Get().ArrangeSet(action)); } diff --git a/Telerik.JustMock/Expectations/AsyncExtensions.cs b/Telerik.JustMock/Expectations/AsyncExtensions.cs index 656a9f47..e53aab18 100644 --- a/Telerik.JustMock/Expectations/AsyncExtensions.cs +++ b/Telerik.JustMock/Expectations/AsyncExtensions.cs @@ -29,7 +29,7 @@ namespace Telerik.JustMock public static partial class AsyncExtensions { /// - /// Specifies the return value for a asynchronous method. + /// Specifies the return value for an asynchronous method. /// /// Type of the return value. /// Reference to interface. @@ -41,9 +41,23 @@ public static IAssertable ReturnsAsync(this FuncExpectation - /// Specifies a function to evaluate and return the value for a asynchronous method. + /// Specifies the return value for an asynchronous method. + /// + /// Type of the return value. + /// Reference to interface. + public static IAssertable ReturnsAsync(this FuncExpectation mock, TResult value) + { + return ProfilerInterceptor.GuardInternal(() => + { + mock.ReturnsAsync(() => value); + return mock; + }); + } + + /// + /// Specifies a function to evaluate and return the value for an asynchronous method. /// /// Type of the return value. /// The function that will evaluate the return value. @@ -61,9 +75,29 @@ public static IAssertable ReturnsAsync(this FuncExpectation + /// Specifies a function to evaluate and return the value for an asynchronous method. + /// + /// Type of the return value. + /// The function that will evaluate the return value. + /// Reference to interface. + public static IAssertable ReturnsAsync(this FuncExpectation mock, Func valueFunction) + { + return ProfilerInterceptor.GuardInternal(() => + { + if (valueFunction == null) + { + return mock.ReturnsAsync(new Func(() => default(TResult))); + } + + return mock.Returns(() => Task.FromResult(valueFunction())); + }); + } + #if NETCORE /// - /// Specifies the return value for a asynchronous method. + /// Specifies the return value for an asynchronous method. /// /// Type of the return value. /// Reference to interface. @@ -77,7 +111,7 @@ public static IAssertable ReturnsAsync(this FuncExpectation - /// Specifies a function to evaluate and return the value for a asynchronous method. + /// Specifies a function to evaluate and return the value for an asynchronous method. /// /// Type of the return value. /// The function that will evaluate the return value. From 2a9b73b10aa2fb89ca6d45bede8bb2b85af85bd7 Mon Sep 17 00:00:00 2001 From: Maria Chervenkova Date: Mon, 7 Oct 2024 18:01:51 +0300 Subject: [PATCH 2/3] fix: Add tests for automocking - Add test for Arrange an ReturnsAsync; --- Telerik.JustMock.Tests/ReturnAsyncFixture.cs | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/Telerik.JustMock.Tests/ReturnAsyncFixture.cs b/Telerik.JustMock.Tests/ReturnAsyncFixture.cs index 21ac339c..eaa2eb04 100644 --- a/Telerik.JustMock.Tests/ReturnAsyncFixture.cs +++ b/Telerik.JustMock.Tests/ReturnAsyncFixture.cs @@ -22,6 +22,7 @@ limitations under the License. using Telerik.JustMock.Helpers; using System.Threading.Tasks; using System.Security.Policy; +using Telerik.JustMock.AutoMock; #region JustMock Test Attributes @@ -61,6 +62,22 @@ public interface ITaskAsync Task GenericTaskWithObjectReturnTypeAndOneParam(object value); } + public class TaskClient + { + private ITaskAsync task; + + public TaskClient(ITaskAsync t) + { + task = t; + } + + public async Task TaskUsageWithValue() + { + return await task.GenericTaskWithValueReturnTypeAndOneParam(10); + } + } + + #if NETCORE public interface IValueTaskAsync { @@ -158,6 +175,38 @@ public async Task ShouldReturnAsyncValueForGenericTaskWithObjectReturnTypeAndOne Assert.Same(expected, result); } + [TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")] + public async Task TestTaskAutomockingReturnsAsyncResult() + { + // Arange + var container = new MockingContainer(); + var expectedResult = 10; + + container.Arrange(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult); + + // Act + var actualResult = await container.Instance.TaskUsageWithValue(); + + // Assert + Assert.Equals(expectedResult, actualResult); + } + + [TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")] + public async Task TestTaskAutomockingAsyncResultWithIntParameter() + { + // Arange + var container = new MockingContainer(); + var expectedResult = 10; + + container.Arrange(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult); + + // Act + var actualResult = await container.Instance.TaskUsageWithValue(); + + // Assert + Assert.Equals(expectedResult, actualResult); + } + #if NETCORE [TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")] public async Task ShouldReturnAsyncValueForGenericValueTaskWithinAsyncTest() @@ -228,6 +277,22 @@ public async Task ShouldReturnAsyncValueForGenericValueTaskWithObjectReturnTypeA Assert.Same(expected, result); } + + [TestMethod, TestCategory("Lite"), TestCategory("ReturnsAsync")] + public async Task TestValueTaskAutomockingAsyncResultWithIntParameter() + { + // Arange + var container = new MockingContainer(); + var expectedResult = 10; + + container.Arrange(t => t.GenericTaskWithValueReturnTypeAndOneParam(Arg.AnyInt)).ReturnsAsync(expectedResult); + + // Act + var actualResult = await container.Instance.TaskUsageWithValue(); + + // Assert + Assert.Equals(expectedResult, actualResult); + } #endif } } From 7605893f5df66ea37b8e7ddf46e157b883cc4e85 Mon Sep 17 00:00:00 2001 From: Maria Chervenkova Date: Tue, 8 Oct 2024 17:11:38 +0300 Subject: [PATCH 3/3] chore: Change copyright information --- Telerik.JustMock.Tests/ReturnAsyncFixture.cs | 2 +- Telerik.JustMock/AutoMock/MockingContainer.cs | 2 +- Telerik.JustMock/Expectations/AsyncExtensions.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Telerik.JustMock.Tests/ReturnAsyncFixture.cs b/Telerik.JustMock.Tests/ReturnAsyncFixture.cs index eaa2eb04..3d450173 100644 --- a/Telerik.JustMock.Tests/ReturnAsyncFixture.cs +++ b/Telerik.JustMock.Tests/ReturnAsyncFixture.cs @@ -1,6 +1,6 @@ /* JustMock Lite - Copyright © 2022 Progress Software Corporation + Copyright © 2022, 2024 Progress Software Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/Telerik.JustMock/AutoMock/MockingContainer.cs b/Telerik.JustMock/AutoMock/MockingContainer.cs index 9aba960d..8450ca45 100644 --- a/Telerik.JustMock/AutoMock/MockingContainer.cs +++ b/Telerik.JustMock/AutoMock/MockingContainer.cs @@ -1,6 +1,6 @@ /* JustMock Lite - Copyright © 2010-2015,2018-2019 Progress Software Corporation + Copyright © 2010-2015,2018-2019, 2024 Progress Software Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/Telerik.JustMock/Expectations/AsyncExtensions.cs b/Telerik.JustMock/Expectations/AsyncExtensions.cs index e53aab18..143f369e 100644 --- a/Telerik.JustMock/Expectations/AsyncExtensions.cs +++ b/Telerik.JustMock/Expectations/AsyncExtensions.cs @@ -1,6 +1,6 @@ /* JustMock Lite - Copyright © 2022 Progress Software Corporation + Copyright © 2022, 2024 Progress Software Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.