How to use TestBed class of Telerik.JustMock.Tests package

Best JustMockLite code snippet using Telerik.JustMock.Tests.TestBed

Run JustMockLite automation tests on LambdaTest cloud grid

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

DynamicFixture.cs

Source: DynamicFixture.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015 Progress Software Corporation
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 System.Linq;
20using System.Collections.Generic;
21
22
23
24#region JustMock Test Attributes
25#if NUNIT
26using NUnit.Framework;
27using TestCategory = NUnit.Framework.CategoryAttribute;
28using TestClass = NUnit.Framework.TestFixtureAttribute;
29using TestMethod = NUnit.Framework.TestAttribute;
30using TestInitialize = NUnit.Framework.SetUpAttribute;
31using TestCleanup = NUnit.Framework.TearDownAttribute;
32using AssertionException = NUnit.Framework.AssertionException;
33#elif XUNIT
34using Xunit;
35using Telerik.JustMock.XUnit.Test.Attributes;
36using TestCategory = Telerik.JustMock.XUnit.Test.Attributes.XUnitCategoryAttribute;
37using TestClass = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute;
38using TestMethod = Xunit.FactAttribute;
39using TestInitialize = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestInitializeAttribute;
40using TestCleanup = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestCleanupAttribute;
41using AssertionException = Telerik.JustMock.XUnit.AssertFailedException;
42#elif VSTEST_PORTABLE
43using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
44using AssertionException = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException;
45#else
46using Microsoft.VisualStudio.TestTools.UnitTesting;
47using AssertionException = Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException;
48#endif
49#endregion
50
51
52namespace Telerik.JustMock.Tests
53{
54	[TestClass]
55	public class DynamicFixture
56	{
57		public class TestBed
58		{
59			protected virtual int Value
60			{
61				get { throw new NotImplementedException(); }
62				set { throw new NotImplementedException(); }
63			}
64
65			protected virtual int Get(int x, string y)
66			{
67				throw new NotImplementedException();
68			}
69
70			protected virtual int this[string x]
71			{
72				get { throw new NotImplementedException(); }
73				set { throw new NotImplementedException(); }
74			}
75
76			protected virtual INode Root
77			{
78				get { throw new NotImplementedException(); }
79				set { throw new NotImplementedException(); }
80			}
81
82			protected virtual T Get<T>()
83			{
84				throw new NotImplementedException();
85			}
86
87			protected virtual ICollection<T> Digest<T>(IList<T> value)
88			{
89				return value;
90			}
91
92			public class Accessor
93			{
94				private readonly TestBed testBed;
95
96				public Accessor(TestBed testBed)
97				{
98					this.testBed = testBed;
99				}
100
101				public int Value
102				{
103					get { return this.testBed.Value; }
104					set { this.testBed.Value = value; }
105				}
106
107				public int Get(int x, string y)
108				{
109					return this.testBed.Get(x, y);
110				}
111
112				public T Get<T>()
113				{
114					return this.testBed.Get<T>();
115				}
116
117				public ICollection<T> Digest<T>(IList<T> x)
118				{
119					return this.testBed.Digest(x);
120				}
121
122				public int this[string x]
123				{
124					get { return this.testBed[x]; }
125					set { this.testBed[x] = value; }
126				}
127
128				public INode Root
129				{
130					get { return this.testBed.Root; }
131					set { this.testBed.Root = value; }
132				}
133			}
134		}
135
136		public interface INode
137		{
138			string Name { get; }
139			INode Left { get; }
140			INode Right { get; }
141		}
142
143		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
144		public void ShouldArrangeNonPublicGetterViaDynaMock()
145		{
146			var mock = Mock.Create<TestBed>();
147			dynamic wrapper = Mock.NonPublic.Wrap(mock);
148			var acc = new TestBed.Accessor(mock);
149
150			Mock.NonPublic.Arrange<int>(wrapper.Value).Returns(123);
151
152			Assert.Equal(123, acc.Value);
153		}
154
155		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
156		public void ShouldArrangeNonPublicSetterViaDynaMock()
157		{
158			var mock = Mock.Create<TestBed>();
159			dynamic wrapper = Mock.NonPublic.Wrap(mock);
160			var acc = new TestBed.Accessor(mock);
161
162			Mock.NonPublic.Arrange(wrapper.Value = 123).MustBeCalled();
163
164			acc.Value = 100;
165			Assert.Throws<AssertionException>(() => Mock.Assert(mock));
166
167			acc.Value = 123;
168
169			Mock.Assert(mock);
170		}
171
172		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
173		public void ShouldArrangeNonPublicSetterWithMatchersViaDynaMock()
174		{
175			var mock = Mock.Create<TestBed>();
176			dynamic wrapper = Mock.NonPublic.Wrap(mock);
177			var acc = new TestBed.Accessor(mock);
178
179			Mock.NonPublic.Arrange(wrapper.Value = Arg.Expr.IsAny<int>()).MustBeCalled();
180			Assert.Throws<AssertionException>(() => Mock.Assert(mock));
181
182			acc.Value = 77;
183
184			Mock.Assert(mock);
185		}
186
187		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
188		public void ShouldArrangeNonPublicMethodViaDynaMock()
189		{
190			var mock = Mock.Create<TestBed>();
191			dynamic wrapper = Mock.NonPublic.Wrap(mock);
192			var acc = new TestBed.Accessor(mock);
193
194			Mock.NonPublic.Arrange<int>(wrapper.Get(10, "ss")).Returns(123);
195
196			Assert.Equal(0, acc.Get(20, "dd"));
197			Assert.Equal(123, acc.Get(10, "ss"));
198		}
199
200		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
201		public void ShouldArrangeNonPublicMethodWithMatchersViaDynaMock()
202		{
203			var mock = Mock.Create<TestBed>();
204			dynamic wrapper = Mock.NonPublic.Wrap(mock);
205			var acc = new TestBed.Accessor(mock);
206
207			Mock.NonPublic.Arrange<int>(wrapper.Get(Arg.Expr.Matches<int>(x => x > 40), Arg.Expr.IsAny<string>())).Returns(123);
208
209			Assert.Equal(0, acc.Get(20, "ss"));
210			Assert.Equal(123, acc.Get(50, "dd"));
211		}
212
213		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
214		public void ShouldArrangeNonPublicIndexerGetterViaDynaMock()
215		{
216			var mock = Mock.Create<TestBed>();
217			dynamic wrapper = Mock.NonPublic.Wrap(mock);
218			var acc = new TestBed.Accessor(mock);
219
220			Mock.NonPublic.Arrange<int>(wrapper["sss"]).Returns(123);
221
222			Assert.Equal(0, acc["ssd"]);
223			Assert.Equal(123, acc["sss"]);
224		}
225
226		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
227		public void ShouldArrangeNonPublicIndexerSetterViaDynaMock()
228		{
229			var mock = Mock.Create<TestBed>();
230			dynamic wrapper = Mock.NonPublic.Wrap(mock);
231			var acc = new TestBed.Accessor(mock);
232
233			Mock.NonPublic.Arrange<int>(wrapper["sss"] = 1000).MustBeCalled();
234
235			Assert.Throws<AssertionException>(() => Mock.Assert(mock));
236			acc["sss"] = 123;
237			Assert.Throws<AssertionException>(() => Mock.Assert(mock));
238			acc["aaa"] = 1000;
239			Assert.Throws<AssertionException>(() => Mock.Assert(mock));
240			acc["sss"] = 1000;
241			Mock.Assert(mock);
242		}
243
244		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
245		public void ShouldArrangeNonPublicMemberRecursivelyViaDynaMock()
246		{
247			var mock = Mock.Create<TestBed>();
248			dynamic wrapper = Mock.NonPublic.Wrap(mock);
249			var acc = new TestBed.Accessor(mock);
250
251			Mock.NonPublic.Arrange<string>(wrapper.Root.Left.Left.Right.Name).Returns("abc");
252
253			Assert.Equal("", acc.Root.Left.Name);
254			Assert.Equal("abc", acc.Root.Left.Left.Right.Name);
255		}
256
257		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
258		public void ShouldAssertNonPublicMethodViaDynaMock()
259		{
260			var mock = Mock.Create<TestBed>();
261			dynamic wrapper = Mock.NonPublic.Wrap(mock);
262			var acc = new TestBed.Accessor(mock);
263
264			Assert.Throws<AssertionException>(() => Mock.NonPublic.Assert(wrapper.Value = 123, Occurs.Once()));
265			Assert.Throws<AssertionException>(() => Mock.NonPublic.Assert(wrapper.Value = Arg.Expr.IsAny<int>(), Occurs.Once()));
266
267			acc.Value = 123;
268
269			Mock.NonPublic.Assert(wrapper.Value = 123, Occurs.Once());
270			Mock.NonPublic.Assert(wrapper.Value = Arg.Expr.IsAny<int>(), Occurs.Once());
271		}
272
273#if !COREFX
274		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
275		public void ShouldArrangeNonPublicGenericMethodWithExplicitTypeArgumentsViaDynaMock()
276		{
277			var mock = Mock.Create<TestBed>();
278			dynamic wrapper = Mock.NonPublic.Wrap(mock);
279			var acc = new TestBed.Accessor(mock);
280
281			Mock.NonPublic.Arrange<int>(wrapper.Get<int>()).Returns(123);
282
283			Assert.Equal(123, acc.Get<int>());
284		}
285#endif
286
287		[TestMethod, TestCategory("Lite"), TestCategory("NonPublic"), TestCategory("DynaMock")]
288		public void ShouldArrangeNonPublicGenericMethodViaDynaMock()
289		{
290			var mock = Mock.Create<TestBed>();
291			dynamic wrapper = Mock.NonPublic.Wrap(mock);
292			var acc = new TestBed.Accessor(mock);
293
294			Mock.NonPublic.Arrange<ICollection<int>>(wrapper.Digest(new[] { 123 })).Returns(new[] { 321 });
295
296			Assert.Equal(321, acc.Digest(new[] { 123 }).First());
297		}
298	}
299}
300
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
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)