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

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

Run JustMockLite automation tests on LambdaTest cloud grid

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

FluentFixture.cs

Source: FluentFixture.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 Telerik.JustMock.Core;
20
21#region JustMock Test Attributes
22#if NUNIT
23using NUnit.Framework;
24using TestCategory = NUnit.Framework.CategoryAttribute;
25using TestClass = NUnit.Framework.TestFixtureAttribute;
26using TestMethod = NUnit.Framework.TestAttribute;
27using TestInitialize = NUnit.Framework.SetUpAttribute;
28using TestCleanup = NUnit.Framework.TearDownAttribute;
29using AssertionException = NUnit.Framework.AssertionException;
30#elif XUNIT
31using Xunit;
32using Telerik.JustMock.XUnit.Test.Attributes;
33using TestCategory = Telerik.JustMock.XUnit.Test.Attributes.XUnitCategoryAttribute;
34using TestClass = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute;
35using TestMethod = Xunit.FactAttribute;
36using TestInitialize = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestInitializeAttribute;
37using TestCleanup = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestCleanupAttribute;
38using AssertionException = Telerik.JustMock.XUnit.AssertFailedException;
39#elif VSTEST_PORTABLE
40using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
41using AssertionException = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException;
42#else
43using Microsoft.VisualStudio.TestTools.UnitTesting;
44using AssertionException = Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException;
45#endif
46#endregion
47
48using Telerik.JustMock.Helpers;
49
50#if XUNIT2
51#pragma warning disable xUnit1013 
52#endif
53
54namespace Telerik.JustMock.Tests
55{
56	[TestClass]
57	public class FluentFixture
58	{
59		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
60		public void ShouldArrangeAssertMockUsingFluentInterface()
61		{
62			//Arrange
63			const string baseDir = @"C:\Foo\Sub1\Sub2\Sub3\Sub4";
64			IFileReader fileReader = Mock.Create<IFileReader>();
65
66			fileReader.Arrange(x => x.GetDirectoryParent(baseDir, 4)).Returns(@"C:\Foo\").Occurs(1);
67
68			//Act
69			var handler = new DataFileHandler(fileReader);
70			var parent = handler.GetDirectoryParent(baseDir, 4);
71
72			//Assert
73			Assert.Equal(@"C:\Foo\", parent);
74
75			fileReader.Assert();
76		}
77
78		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
79		public void ShouldAssertActionWhenChained()
80		{
81			IFileReader fileReader = Mock.Create<IFileReader>();
82
83			bool mocked = false;
84
85			fileReader.Arrange(x => x.Delete()).DoInstead(() => mocked = true);
86
87			fileReader.Delete();
88
89			Assert.True(mocked);
90		}
91
92		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
93		public void ShouldAssertPropertyGetWhenChained()
94		{
95			IFileReader fileReader = Mock.Create<IFileReader>();
96
97			const string expected = @"c:\JustMock";
98
99			fileReader.Arrange(x => x.Path).Returns(expected);
100
101			Assert.Equal(fileReader.Path, expected);
102		}
103
104		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
105		public void ShouldAssertPropertySetWhenChained()
106		{
107			IFileReader fileReader = Mock.Create<IFileReader>(Behavior.Strict);
108
109			const string expected = @"c:\JustMock";
110
111			fileReader.ArrangeSet(x => x.Path = expected);
112
113			fileReader.Path = expected;
114
115			Assert.Throws<MockException>(() => fileReader.Path = "abc");
116		}
117
118		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
119		public void ShouldBeAbleToAssertSpecificActionForASetup()
120		{
121			IFileReader fileReader = Mock.Create<IFileReader>();
122
123			fileReader.Arrange(x => x.Delete()).OccursOnce();
124
125			fileReader.Delete();
126
127			fileReader.Assert(x => x.Delete());
128		}
129
130		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
131		public void ShouldBeAbleToAssertSpecificFuntionForASetup()
132		{
133			IFileReader fileReader = Mock.Create<IFileReader>();
134
135			const string expected = @"c:\JustMock";
136
137			fileReader.Arrange(x => x.Path).Returns(expected).OccursOnce();
138
139			Assert.Equal(expected, fileReader.Path);
140
141			fileReader.Assert(x => x.Path);
142		}
143
144		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
145		public void ShouldBeAbleToDoAssertAllForASetup()
146		{
147			IFileReader fileReader = Mock.Create<IFileReader>();
148
149			const string expected = @"c:\JustMock";
150
151			fileReader.Arrange(x => x.Path).Returns(expected);
152
153			Assert.Equal(expected, fileReader.Path);
154
155			fileReader.AssertAll();
156		}
157
158		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
159		public void ShouldCallMethodForDefaultEventWhenRaised()
160		{
161			var foo = Mock.Create<IFileReader>();
162
163			bool raised = false;
164			foo.FileDeleted += (sender, args) => raised = true;
165			foo.Raise(x => x.FileDeleted += null, EventArgs.Empty);
166
167			Assert.True(raised);
168		}
169
170		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
171		public void ShouldInvokeMethodForACustomEventWhenRaised()
172		{
173			var foo = Mock.Create<IFileReader>();
174
175			string actual = string.Empty;
176			foo.FileAdded += (string value) => actual = value;
177			foo.Raise(x => x.FileAdded += null, "x");
178
179			Assert.Equal("x", actual);
180		}
181
182		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
183		public void ShouldFailOnAssertIfOccursNeverInvoked()
184		{
185			var foo = Mock.Create<IFoo>();
186			Mock.Arrange(() => foo.Submit()).Occurs(2);
187
188			Assert.Throws<AssertionException>(() => Mock.Assert(foo));
189		}
190
191		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
192		public void ShouldFailOnAssertIfOccursLessThanExpected()
193		{
194			var foo = Mock.Create<Foo>();
195
196			Mock.Arrange(() => foo.Submit()).Occurs(10);
197
198			foo.Submit();
199
200			Assert.Throws<AssertionException>(() => Mock.Assert(foo));
201		}
202
203		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
204		public void ShouldAssertOccursOnce()
205		{
206			var foo = Mock.Create<IFoo>();
207			Mock.Arrange(() => foo.Submit()).OccursOnce();
208
209			foo.Submit();
210
211			Mock.Assert(foo);
212		}
213
214		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
215		public void ShouldAssertOccursNever()
216		{
217			var foo = Mock.Create<IFoo>();
218
219			Mock.Arrange(() => foo.Submit()).OccursNever();
220
221			Mock.Assert(foo);
222		}
223
224		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
225		public void ShouldAssertOccursAtLeast()
226		{
227			var foo = Mock.Create<IFoo>();
228
229			Mock.Arrange(() => foo.Submit()).OccursAtLeast(2);
230
231			foo.Submit();
232			foo.Submit();
233			foo.Submit();
234
235			Mock.Assert(foo);
236		}
237
238		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
239		public void ShouldFailWhenInvokedMoreThanRequried()
240		{
241			var foo = Mock.Create<IFoo>();
242
243			Mock.Arrange(() => foo.Submit()).OccursAtMost(2);
244
245			foo.Submit();
246			foo.Submit();
247			Assert.Throws<AssertionException>(() => foo.Submit());
248			Assert.Throws<AssertionException>(() => Mock.Assert(foo));
249		}
250
251		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
252		public void ShouldAssertIndividualCallWithLambda()
253		{
254			var foo = Mock.Create<IFoo>();
255
256			Mock.Arrange(() => foo.Submit()).OccursNever();
257
258			Mock.Assert(() => foo.Submit());
259		}
260
261		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
262		public void ShouldAsssertOcurrenceWhenAppliedWithCallOriginal()
263		{
264			var foo = Mock.Create<Foo>(Behavior.CallOriginal);
265
266			Mock.Arrange(() => foo.Submit()).OccursOnce();
267
268			Assert.Throws<AssertionException>(() => Mock.Assert(() => foo.Submit()));
269		}
270
271
272		[TestMethod, TestCategory("Lite"), TestCategory("Fluent"), TestCategory("Occurrence")]
273		public void ShouldFluentAssertOccurrenceExpectationSetInArrange()
274		{
275			const int someValue = 4;
276			var target = Mock.Create<IFoo>();
277			target.Arrange(x => x.Echo(someValue)).OccursNever();
278			target.Assert(x => x.Echo(someValue));
279		}
280
281#if !SILVERLIGHT
282
283		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
284		public void ShouldAssertMockingInternalMember()
285		{
286			var siteOptionsEntity = Mock.Create<HardCodedSiteOptionsEntity>();
287
288			var messageMock = Mock.Create<IMessageOperations>();
289
290			siteOptionsEntity.Arrange(x => x.MessageOperationsHelper).Returns(messageMock);
291
292			Assert.NotNull(siteOptionsEntity.MessageOperationsHelper);
293		}
294
295#endif
296
297		public class HardCodedSiteOptionsEntity
298		{
299			internal virtual IMessageOperations MessageOperationsHelper { get; set; }
300		}
301
302		public interface IMessageOperations
303		{
304
305		}
306
307		public class Foo
308		{
309			public virtual void Submit()
310			{
311
312			}
313		}
314
315		public interface IFoo
316		{
317			void Submit();
318			int Echo(int intArg);
319		}
320
321		public interface IFileReader
322		{
323			string GetDirectoryParent(string directory, int levels);
324			void Delete();
325			string Path { get; set; }
326			event EventHandler<EventArgs> FileDeleted;
327			event CustomEvent FileAdded;
328		}
329
330		public delegate void CustomEvent(string value);
331
332		public class DataFileHandler
333		{
334			readonly IFileReader fileReader;
335
336			public DataFileHandler(IFileReader fileReader)
337			{
338				this.fileReader = fileReader;
339			}
340
341			public string GetDirectoryParent(string directory, int levels)
342			{
343				return fileReader.GetDirectoryParent(directory, levels);
344			}
345		}
346
347		public interface ILinkedListNode
348		{
349			ILinkedListNode Tail { get; }
350		}
351
352		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
353		public void ShouldMockMethodWithReturnTypeSameAsDeclaringClass()
354		{
355			var selectionData = Mock.Create<ILinkedListNode>();
356			Mock.Arrange(() => selectionData.Tail).Returns(null as ILinkedListNode);
357		}
358
359		public interface IDataProcessor
360		{
361			void Process(string data);
362		}
363
364		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
365		public void ShouldAssertWithMatcherWhenMatchedArgumentIsArray()
366		{
367			var mock = Mock.Create<IDataProcessor>();
368			Mock.Arrange(() => mock.Process(Arg.AnyString)).DoNothing();
369
370			var data = new string[] { "abc" };
371			mock.Process(data[0]);
372
373			Mock.Assert(() => mock.Process(data[0]), Occurs.Once());
374		}
375
376		public interface IAction
377		{
378			void Do();
379		}
380
381		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
382		public void ShouldFailToChainReturnsCallToVoidMethod()
383		{
384			var mock = Mock.Create<IAction>();
385
386			Assert.Throws<MockException>(() => Mock.Arrange(() => mock.Do()).Returns(123));
387		}
388
389		public interface IGuidResolver
390		{
391			Guid? GetGuid(string id);
392		}
393
394		[TestMethod, TestCategory("Lite"), TestCategory("Fluent")]
395		public void ShouldFailToChainReturnsCallToActionExpectationFromNonPublicInterface()
396		{
397			var mock = Mock.Create<IGuidResolver>();
398			Assert.Throws<MockException>(() => Mock.NonPublic.Arrange(mock, "GetGuid", Arg.Expr.IsNull<string>()).Returns((Guid?)new Guid()));
399		}
400	}
401
402	[TestClass]
403	public class FluentContextFixture
404	{
405		IDisposable mock;
406
407#if XUNIT
408		public FluentContextFixture()
409		{
410			TestInit();
411		}
412#endif
413		[TestInitialize]
414		public void TestInit()
415		{
416			mock = Mock.Create<IDisposable>();
417		}
418
419		[TestMethod]
420		public void ShouldUpdateContextInFluentAssert()
421		{
422			Mock.Arrange(() => mock.Dispose());
423		}
424
425		[TestCleanup]
426		public void TestCleanup()
427		{
428			mock.AssertAll();
429			Mock.AssertAll(mock);
430		}
431	}
432}
433
434#if XUNIT2
435#pragma warning restore xUnit1013 
436#endif
437
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)