How to use GetInteger method of JustMock.NonElevatedExamples.AdvancedUsage.MockingDelegates.Foo class

Best JustMockLite code snippet using JustMock.NonElevatedExamples.AdvancedUsage.MockingDelegates.Foo.GetInteger

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

MockingDelegates.cs

Source: MockingDelegates.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2014 Telerik EAD
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16*/
17
18using System;
19using Microsoft.VisualStudio.TestTools.UnitTesting;
20using Telerik.JustMock;
21
22namespace JustMock.NonElevatedExamples.AdvancedUsage.MockingDelegates
23{
24    /// <summary>
25    /// With Telerik JustMock you can mock delegates and additionally apply all mock capabilities on them. For example, you can 
26    ///  assert against their invocation, arrange certain expectations and then pass them in the system under test.
27    /// See http://www.telerik.com/help/justmock/advanced-usage-mocking-delegates.html for full documentation of the feature.
28    /// </summary>
29    [TestClass]
30    public class MockingDelegates_Tests
31    {
32        [TestMethod]
33        public void ShouldArrangeReturnExpectation()
34        {
35            // ARRANGE
36            // Creating a mock instance of the Func<int, int> delegate.
37            var delegateMock = Mock.Create<Func<int, int>>();
38            
39            // Arranging: When the mock is called with 10 as an integer argument, it should return 20.
40            Mock.Arrange(() => delegateMock(10)).Returns(20);
41
42            // ACT
43            var mySUT = new Foo();
44            // Assigning the mock to the dependent property.
45            mySUT.FuncDelegate = delegateMock;
46            var actual = mySUT.GetInteger(10);
47            
48            // ASSERT
49            Assert.AreEqual(20, actual);
50        }
51
52        [TestMethod]
53        public void ShouldArrangeOccurrenceExpectation()
54        {
55            // ARRANGE
56            // Creating a mock instance of the Func<int, int> delegate.
57            var delegateMock = Mock.Create<Func<int, int>>();
58
59            // Arranging: That the mock should be called with any integer values during the test execution.
60            Mock.Arrange(() => delegateMock(Arg.AnyInt)).MustBeCalled();
61
62            // ACT
63            var mySUT = new Foo();
64            mySUT.FuncDelegate = delegateMock;
65            // Assigning the mock to the dependent property.
66            var actual = mySUT.GetInteger(123);
67
68            // ASSERT - asserting the mock.
69            Mock.Assert(delegateMock);
70        }
71
72        [TestMethod]
73        public void ShouldPassPrearrangedDelegateMockAsArgument()
74        {
75            // ARRANGE
76            // Creating a mock instance of the Func<string> delegate.
77            var delegateMock = Mock.Create<Func<string>>();
78
79            // Arranging: When the mock is called, it should return "Success".
80            Mock.Arrange(() => delegateMock()).Returns("Success");
81
82            // ACT
83            var testInstance = new DataRepository();
84            // Passing the mock into our system under test.
85            var actual = testInstance.GetCurrentUserId(delegateMock);
86
87            // ASSERT
88            Assert.AreEqual("Success", actual);
89        }
90
91        [TestMethod]
92        public void ShouldPassDelegateMockAsArgumentAndAssertItsOccurrence()
93        {
94            bool isCalled = false;
95
96            // ARRANGE
97            // Creating a mock instance of the Action<int> delegate.
98            var delegateMock = Mock.Create<Action<int>>();
99
100            // Arranging: When the mock is called with any integer value as an argument, it should assign true to isCalled instead.
101            Mock.Arrange(() => delegateMock(Arg.AnyInt)).DoInstead(() => isCalled = true);
102
103            // ACT
104            var testInstance = new DataRepository();
105            // Passing the mock into our system under test.
106            testInstance.ApproveCredentials(delegateMock);
107
108            // ASSERT
109            Assert.IsTrue(isCalled);
110        }
111    }
112
113    #region SUT
114    public class DataRepository
115    {
116        public string GetCurrentUserId(Func<string> callback)
117        {
118            return callback();
119        }
120
121        public void ApproveCredentials(Action<int> callback)
122        {
123            // Some logic here...
124
125            callback(1);
126        }
127    }
128
129    public class Foo
130    {
131        public Func<int, int> FuncDelegate { get; set; }
132
133        public int GetInteger(int toThisInt)
134        {
135            return FuncDelegate(toThisInt);
136        }
137    }
138    #endregion
139}
140
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in Foo

Trigger GetInteger code on LambdaTest Cloud Grid

Execute automation tests with GetInteger on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)