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

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

Run JustMockLite automation tests on LambdaTest cloud grid

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

MockFixture.cs

Source: MockFixture.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015,2019 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
18
19using System;
20using System.Reflection;
21using System.Collections.Generic;
22using System.Linq;
23using System.Linq.Expressions;
24using System.Runtime.InteropServices;
25using System.Security.Principal;
26using Telerik.JustMock.Core;
27
28#if LITE_EDITION
29#if !COREFX
30using Telerik.JustMock.DemoLibSigned;
31#endif
32#endif
33
34#region JustMock Test Attributes
35#if NUNIT
36using NUnit.Framework;
37using TestCategory = NUnit.Framework.CategoryAttribute;
38using TestClass = NUnit.Framework.TestFixtureAttribute;
39using TestMethod = NUnit.Framework.TestAttribute;
40using TestInitialize = NUnit.Framework.SetUpAttribute;
41using TestCleanup = NUnit.Framework.TearDownAttribute;
42using AssertionException = NUnit.Framework.AssertionException;
43#elif XUNIT
44using Xunit;
45using Telerik.JustMock.XUnit.Test.Attributes;
46using TestCategory = Telerik.JustMock.XUnit.Test.Attributes.XUnitCategoryAttribute;
47using TestClass = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute;
48using TestMethod = Xunit.FactAttribute;
49using TestInitialize = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestInitializeAttribute;
50using TestCleanup = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestCleanupAttribute;
51using AssertionException = Telerik.JustMock.XUnit.AssertFailedException;
52#elif VSTEST_PORTABLE
53using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
54using AssertionException = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException;
55#else
56using Microsoft.VisualStudio.TestTools.UnitTesting;
57using AssertionException = Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException;
58#endif
59#endregion
60
61
62namespace Telerik.JustMock.Tests
63{
64	/// <summary>
65	/// Validates Mock capabilities
66	/// </summary>
67	[TestClass]
68	public class MockFixture
69	{
70		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
71		public void ShouldMockInterface()
72		{
73			var iCloneable = Mock.Create<ICloneable>();
74			Assert.NotNull(iCloneable);
75		}
76
77		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
78		public void ShouldMockInterfaceContainingProperty()
79		{
80			var bar = Mock.Create<IBar>();
81			Assert.NotNull(bar);
82		}
83
84		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
85		public void ShouldReturnServiceFromServiceProvider()
86		{
87			var provider = Mock.Create<IServiceProvider>();
88
89			Mock.Arrange(() => provider.GetService(typeof(IFooService))).Returns(new FooService());
90
91			Assert.True(provider.GetService(typeof(IFooService)) is FooService);
92		}
93
94		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
95		public void ShouldAssertDoInstead()
96		{
97			var foo = Mock.Create<IFoo>();
98
99			bool called = false;
100
101			Mock.Arrange(() => foo.Execute(Arg.IsAny<string>()))
102								  .DoInstead(() => { called = true; })
103								  .Returns((string s) => s);
104
105			Assert.Equal(foo.Execute("ping"), "ping");
106			Assert.True(called);
107		}
108
109		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
110		public void ShouldAssertDoInsteadForOneArgFromMethod()
111		{
112			int expected = 0;
113
114			var foo = Mock.Create<IFoo>();
115			Mock.Arrange(() => foo.Submit(Arg.IsAny<int>()))
116				.DoInstead((int arg1) => { expected = arg1; });
117
118			foo.Submit(10);
119
120			Assert.Equal(10, expected);
121		}
122
123		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
124		public void ShouldAssertDoInsteadForTwoArgsFromMethod()
125		{
126			int expected = 0;
127
128			var foo = Mock.Create<IFoo>();
129			Mock.Arrange(() => foo.Submit(Arg.IsAny<int>(), Arg.IsAny<int>()))
130				.DoInstead((int arg1, int arg2) => { expected = arg1 + arg2; });
131
132			foo.Submit(10, 10);
133
134			Assert.Equal(20, expected);
135		}
136
137
138		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
139		public void ShouldAssertDoInsteadForThreeArgsFromMethod()
140		{
141			int expected = 0;
142
143			var foo = Mock.Create<IFoo>();
144			Mock.Arrange(() => foo.Submit(Arg.IsAny<int>(), Arg.IsAny<int>(), Arg.IsAny<int>()))
145				.DoInstead((int arg1, int arg2, int arg3) => { expected = arg1 + arg2 + arg3; });
146
147			foo.Submit(10, 10, 10);
148
149			Assert.Equal(30, expected);
150		}
151
152
153		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
154		public void ShouldAssertDoInsteadForFourArgsFromMethod()
155		{
156			int expected = 0;
157
158			var foo = Mock.Create<IFoo>();
159			Mock.Arrange(() => foo.Submit(Arg.IsAny<int>(), Arg.IsAny<int>(), Arg.IsAny<int>(), Arg.IsAny<int>()))
160				.DoInstead((int arg1, int arg2, int arg3, int arg4) => { expected = arg1 + arg2 + arg3 + arg4; });
161
162			foo.Submit(10, 10, 10, 10);
163
164			Assert.Equal(40, expected);
165		}
166
167		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
168		public void ShouldAssertDoInsteadForOneArgFromFunc()
169		{
170			int expected = 0;
171
172			var foo = Mock.Create<IFoo>();
173			Mock.Arrange(() => foo.Echo(Arg.IsAny<int>()))
174				.DoInstead((int arg1) => { expected = arg1; })
175				.Returns(() => expected);
176
177			Assert.Equal(foo.Echo(10), expected);
178		}
179
180
181		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
182		public void ShouldAssertDoInsteadForTwoArgsFromFunc()
183		{
184			int expected = 0;
185
186			var foo = Mock.Create<IFoo>();
187			Mock.Arrange(() => foo.Echo(Arg.IsAny<int>(), Arg.IsAny<int>()))
188				.DoInstead((int arg1, int arg2) => { expected = arg1 + arg2; })
189				.Returns(() => expected);
190
191			Assert.Equal(foo.Echo(10), expected);
192		}
193
194		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
195		public void ShouldAssertDoInsteadForThreeArgsFromFunc()
196		{
197			int expected = 0;
198
199			var foo = Mock.Create<IFoo>();
200			Mock.Arrange(() => foo.Echo(Arg.IsAny<int>(), Arg.IsAny<int>(), Arg.IsAny<int>()))
201				.DoInstead((int arg1, int arg2, int arg3) => { expected = arg1 + arg2 + arg3; })
202				.Returns(() => expected);
203
204			Assert.Equal(foo.Echo(10), expected);
205		}
206
207		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
208		public void ShouldAssertDoInsteadForFourArgsFromFunc()
209		{
210			int expected = 0;
211
212			var foo = Mock.Create<IFoo>();
213			Mock.Arrange(() => foo.Echo(Arg.IsAny<int>(), Arg.IsAny<int>(), Arg.IsAny<int>(), Arg.IsAny<int>()))
214				.DoInstead((int arg1, int arg2, int arg3, int arg4) => { expected = arg1 + arg2 + arg3 + arg4; })
215				.Returns(() => expected);
216
217			Assert.Equal(foo.Echo(10), expected);
218		}
219
220		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
221		public void ShouldThrowIfArgumentsPassedForInterface()
222		{
223			Assert.Throws<Exception>(() => Mock.Create<IFoo>(25, true));
224		}
225
226		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
227		public void ShouldAssertCallOriginal()
228		{
229			var foo = Mock.Create<FooBase>();
230
231			Mock.Arrange(() => foo.GetString("x")).CallOriginal();
232			Mock.Arrange(() => foo.GetString("y")).Returns("z");
233
234			Assert.Equal("x", foo.GetString("x"));
235			Assert.Equal("z", foo.GetString("y"));
236		}
237
238		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
239		public void ShouldAssertGuidParam()
240		{
241			var foo = Mock.Create<IFoo>();
242
243			Guid guid = Guid.NewGuid();
244
245			bool called = false;
246
247			Mock.Arrange(() => foo.Execute(guid)).DoInstead(() => called = true);
248
249			foo.Execute(guid);
250
251			Assert.True(called);
252		}
253
254		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
255		public void ShouldAssertCallOriginalForVoid()
256		{
257			var log = Mock.Create<Log>();
258			Mock.Arrange(() => log.Info(Arg.IsAny<string>())).CallOriginal();
259			Assert.Throws<Exception>(() => log.Info("test"));
260		}
261
262		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
263		public void ShouldPassArgumentToOriginal()
264		{
265			var log = Mock.Create<Log>();
266
267			Mock.Arrange(() => log.Info("x")).CallOriginal();
268
269			Assert.Equal(Assert.Throws<Exception>(() => log.Info("x")).Message, "x");
270		}
271
272		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
273		public void MockObjectShouldBeAssignableToMockedInterface()
274		{
275			var iFoo = Mock.Create<IFoo>();
276			Assert.True(iFoo is IFoo);
277		}
278
279		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
280		public void ShouldAssertVoidCall()
281		{
282			var iFoo = Mock.Create<IFoo>();
283
284			Mock.Arrange(() => iFoo.JustCall()).DoNothing();
285
286			iFoo.JustCall();
287		}
288
289		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
290		public void ShouldAssertCallsThatAreMethodCalls()
291		{
292			const int arg = 2;
293			var iBar = Mock.Create<IBar>();
294			Mock.Arrange(() => iBar.Echo(Arg.IsAny<int>())).Returns(1);
295
296			Assert.Equal(iBar.Echo(arg + 1), 1);
297		}
298
299
300		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
301		public void ShouldAssertGenericThrowsCall()
302		{
303			var iFoo = Mock.Create<IFoo>();
304
305			Mock.Arrange(() => iFoo.Execute(Arg.IsAny<string>())).Throws<FormatException>();
306			Assert.Throws<FormatException>(() => iFoo.Execute("crash"));
307		}
308
309		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
310		public void ShouldAssertThrowsHavingArguments()
311		{
312			var iFoo = Mock.Create<IFoo>();
313
314			Mock.Arrange(() => iFoo.Execute(Arg.IsAny<string>())).Throws<CustomExepction>("test", true);
315			Assert.Throws<CustomExepction>(() => iFoo.Execute("crash"));
316		}
317
318
319		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
320		public void ShouldAssertEqualityRefereces()
321		{
322			var iFoo1 = Mock.Create<IFoo>();
323			var iFoo2 = Mock.Create<IFoo>();
324
325			Assert.True(iFoo1.Equals(iFoo1));
326			Assert.False(iFoo1.Equals(iFoo2));
327		}
328
329		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
330		public void TwoMockedObjectsShouldHavdDifferentHashCode()
331		{
332			var iFoo1 = Mock.Create<IFoo>();
333			var iFoo2 = Mock.Create<IFoo>();
334			Assert.NotEqual(iFoo1.GetHashCode(), iFoo2.GetHashCode());
335		}
336
337		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
338		public void ToStringShouldNotbeNullOrEmpty()
339		{
340			var foo = Mock.Create<IFoo>();
341			Assert.False(String.IsNullOrEmpty(foo.ToString()));
342		}
343
344		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
345		public void ShouldMockClassWithNonDefaultConstructor()
346		{
347			var nonDefaultClass = Mock.Create<ClassWithNonDefaultConstructor>("ping", 1);
348			Assert.NotNull(nonDefaultClass);
349		}
350
351		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
352		public void ShouldMockClassWithNonDefaultConstructorWithoutPassingAnything()
353		{
354			var nonDefaultClass = Mock.Create<ClassWithNonDefaultConstructor>();
355			Assert.NotNull(nonDefaultClass);
356		}
357
358		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
359		public void ShouldMockClassWithNoDefaultConstructorWithNull()
360		{
361			var nonDefaultClass = Mock.Create<ClassWithNonDefaultConstructor>(null, 1);
362			Assert.NotNull(nonDefaultClass);
363		}
364
365		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
366		public void ShoulsMockClassWithMultipleNonDefaultConstructor()
367		{
368			var nonDefaultClass = Mock.Create<ClassWithNonDefaultConstructor>(null, 1, true);
369			Assert.NotNull(nonDefaultClass);
370		}
371
372		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
373		public void ShouldAssertGuidNonDefaultCtorWithDefaultIfNotSpecified()
374		{
375			var nonDefaultGuidClass = Mock.Create<ClassNonDefaultGuidConstructor>();
376			Assert.Equal(nonDefaultGuidClass.guidValue, default(Guid));
377		}
378
379		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
380		public void ShouldAssertBaseCallWithGuid()
381		{
382			var foo = Mock.Create<FooBase>();
383
384			Mock.Arrange(() => foo.GetGuid()).CallOriginal();
385
386			Assert.Equal(foo.GetGuid(), default(Guid));
387		}
388
389		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
390		public void ShouldAssertImplementedInterface()
391		{
392			var implemented = Mock.Create<IFooImplemted>();
393			Assert.NotNull(implemented);
394		}
395
396		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
397		public void ShouldAssertBaseForImplemetedInterface()
398		{
399			var implemented = Mock.Create<IFooImplemted>();
400			Assert.True(implemented is IFoo);
401		}
402
403		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
404		public void SHouldAssertCallsFromBaseInImplemented()
405		{
406			var implemented = Mock.Create<IFooImplemted>();
407
408			Mock.Arrange(() => implemented.Execute("hello")).Returns("world");
409
410			Assert.Equal(implemented.Execute("hello"), "world");
411		}
412
413		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
414		public void ShouldMockObject_GetHashCodeMethod()
415		{
416			var foo = Mock.Create<FooBase>();
417
418			Mock.Arrange(() => foo.GetHashCode()).Returns(1);
419
420			Assert.Equal(1, foo.GetHashCode());
421		}
422
423		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
424		public void ShouldMockObject_ToStringMethod()
425		{
426			var foo = Mock.Create<FooBase>();
427			Mock.Arrange(() => foo.ToString()).Returns("foo");
428			Assert.Equal("foo", foo.ToString());
429		}
430
431		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
432		public void ShouldMockObject_EqualsMethod()
433		{
434			var foo = Mock.Create<FooBase>();
435			Mock.Arrange(() => foo.Equals(Arg.IsAny<object>())).Returns(true);
436			Assert.True(foo.Equals(new object()));
437		}
438
439		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
440		public void ShouldCallUnderlyingEquals()
441		{
442			var foo1 = Mock.Create<FooOverridesEquals>("foo");
443			var foo2 = Mock.Create<FooOverridesEquals>("foo");
444			Assert.True(foo1.Equals(foo2));
445		}
446
447		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
448		public void ShouldNotCallBaseByDefault()
449		{
450			var foo = Mock.Create<FooBase>();
451			// this will not throw exception.
452			foo.ThrowException();
453		}
454
455		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
456		public void ShouldTakeLatestSetup()
457		{
458			var foo = Mock.Create<IFoo>();
459
460			Mock.Arrange(() => foo.Execute("ping")).Returns("pong");
461			Mock.Arrange(() => foo.Execute(Arg.IsAny<string>())).Returns("pong");
462
463			Assert.Equal(foo.Execute("nothing"), "pong");
464		}
465
466		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
467		public void ShouldOverrideBehaviorFromBaseClass()
468		{
469			var foo = Mock.Create<FooBase>();
470
471			Mock.Arrange(() => foo.GetString("pong")).CallOriginal().InSequence();
472			Mock.Arrange(() => foo.GetString(Arg.IsAny<string>())).Returns("ping").InSequence();
473
474			Assert.Equal(foo.GetString("pong"), "pong");
475			Assert.Equal(foo.GetString("it"), "ping");
476		}
477
478		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
479		public void ShouldAssertDateTimeAsRef()
480		{
481			var foo = Mock.Create<IFoo>();
482
483			DateTime expected = new DateTime(2009, 11, 26);
484
485			Mock.Arrange(() => foo.Execute(out expected)).DoNothing();
486
487			DateTime acutal = DateTime.Now;
488
489			foo.Execute(out acutal);
490
491			Assert.Equal(expected, acutal);
492		}
493
494		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
495		public void ShouldAssertOutputParamPassedViaNested()
496		{
497			var nested = new Nested();
498			nested.expected = 10;
499
500			var foo = Mock.Create<IFoo>();
501
502			Mock.Arrange(() => foo.Execute(out nested.expected));
503
504			int actual = 0;
505
506			foo.Execute(out actual);
507
508			Assert.Equal(actual, 10);
509		}
510
511		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
512		public void ShouldNotInvokeOriginalCallWhenInitiatedFromCtor()
513		{
514			var foo = Mock.Create<FooAbstractCall>(false);
515			Assert.NotNull(foo);
516		}
517
518		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
519		public void ShouldNotInvokeOriginalActionWhenInitiatedFromCtor()
520		{
521			var foo = Mock.Create<FooAbstractAction>();
522			Assert.NotNull(foo);
523		}
524
525		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
526		public void ShouldNotInitalizeAsArgMatcherWhenProcessingMemberAccessArgument()
527		{
528			var foo = Mock.Create<IFoo>();
529
530			Mock.Arrange(() => foo.Execute(false, BadGuid)).OccursOnce();
531
532			foo.Execute(false, Guid.Empty);
533
534			Mock.Assert(() => foo.Execute(Arg.IsAny<bool>(), Guid.Empty));
535		}
536
537		public static readonly Guid BadGuid = Guid.Empty;
538
539		public abstract class FooAbstractCall
540		{
541			public FooAbstractCall(bool flag)
542			{
543				// invoke base will throw exception here.
544				Initailize();
545			}
546
547			public abstract bool Initailize();
548		}
549
550		public abstract class FooAbstractAction
551		{
552			public FooAbstractAction()
553			{
554				// invoke base will throw exception here.
555				Initailize();
556			}
557
558			public abstract void Initailize();
559		}
560
561
562#if !COREFX
563		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
564		public void ShouldCreateMockClassWithInternalConstructor()
565		{
566			var foo = Mock.Create<FooWithInternalConstruct>();
567			Assert.NotNull(foo);
568		}
569
570		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
571		public void ShouldAssertArrangeWithInternalConstructor()
572		{
573			var foo = Mock.Create<FooWithInternalConstruct>();
574
575			bool called = false;
576
577			Mock.Arrange(() => foo.Execute()).DoInstead(() => called = true);
578
579			foo.Execute();
580
581			Assert.True(called);
582		}
583#endif
584
585		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
586		public void ShouldAssertGenericFuncCalls()
587		{
588			var genericClass = Mock.Create<FooGeneric<int>>();
589
590			Mock.Arrange(() => genericClass.Get(1, 1)).Returns(10);
591
592			Assert.Equal(genericClass.Get(1, 1), 10);
593		}
594
595		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
596		public void ShouldAssertGenericVoidCalls()
597		{
598			var genericClass = Mock.Create<FooGeneric<int>>();
599
600			bool called = false;
601
602			Mock.Arrange(() => genericClass.Execute(1)).DoInstead(() => called = true);
603
604			genericClass.Execute(1);
605
606			Assert.True(called);
607		}
608
609		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
610		public void ShouldAssertGenricMockWithNoGenericClass()
611		{
612			var genericClass = Mock.Create<FooGeneric>();
613
614			Mock.Arrange(() => genericClass.Get<int, int>(1)).Returns(10);
615
616			Assert.Equal(genericClass.Get<int, int>(1), 10);
617		}
618
619		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
620		public void ShouldAssertFuncWithOccurrence()
621		{
622			var foo = Mock.Create<IFoo>();
623
624			Mock.Arrange(() => foo.Execute("x")).Returns("x");
625
626			foo.Execute("x");
627
628			Mock.Assert(() => foo.Execute("x"), Occurs.Exactly(1));
629		}
630
631
632		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
633		public void ShouldAssertOutputGenericArgument()
634		{
635			var fooGen = Mock.Create<FooGeneric>();
636
637			int result = 0;
638
639			fooGen.Execute<int, int>(out result);
640
641			Assert.Equal(result, 0);
642		}
643
644		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
645		public void ShouldAssertArrangeForGenricOutArgument()
646		{
647			var fooGen = Mock.Create<FooGeneric>();
648
649			int expected = 10;
650
651			Mock.Arrange(() => fooGen.Execute<int, int>(out expected)).Returns(0);
652
653			int actual = 0;
654
655			fooGen.Execute<int, int>(out actual);
656
657			Assert.Equal(expected, actual);
658		}
659
660		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
661		public void ShouldAssertParamArrayMatchArugments()
662		{
663			string expected = "bar";
664			string argument = "foo";
665
666			var target = Mock.Create<IParams>();
667
668			Mock.Arrange(() => target.ExecuteParams(argument, "baz")).Returns(expected);
669			string ret = target.ExecuteParams(argument, "baz");
670			Assert.Equal(expected, ret);
671		}
672
673		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
674		public void ShouldDistinguiseMethodWithDifferentGenericArgument()
675		{
676			var foo = Mock.Create<FooGeneric>();
677
678			Mock.Arrange(() => foo.Get<int>()).Returns(10);
679			Mock.Arrange(() => foo.Get<string>()).Returns(12);
680
681			Assert.Equal(foo.Get<string>(), 12);
682			Assert.Equal(foo.Get<int>(), 10);
683		}
684
685		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
686		public void ShouldAssertParamArrayWithMatcherAndConcreteValue()
687		{
688			string expected = "bar";
689			string argument = "foo";
690
691			var target = Mock.Create<IParams>();
692
693			Mock.Arrange(() => target.ExecuteByName(Arg.IsAny<int>(), argument)).Returns(expected);
694
695			string ret = target.ExecuteByName(0, argument);
696
697			Assert.Equal(expected, ret);
698		}
699
700
701		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
702		public void ShouldAssertCallWithMultipleMathers()
703		{
704			var foo = Mock.Create<IFoo>();
705
706			Mock.Arrange(() => foo.Echo(Arg.Matches<int>(x => x == 10), Arg.Matches<int>(x => x == 10))).Returns(20);
707
708			int ret = foo.Echo(10, 10);
709
710			Assert.Equal(20, ret);
711		}
712
713		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
714		public void ShouldAssertArgumentMatherArgumentSetup()
715		{
716			var foo = Mock.Create<IFoo>();
717			Mock.Arrange(() => foo.Echo(10, Arg.Matches<int>(x => x > 10 && x < 20), 21)).Returns(20);
718
719			int ret = foo.Echo(10, 11, 21);
720
721			Assert.Equal(20, ret);
722		}
723
724
725		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
726		public void ShouldAssertCallWithDefaultValues()
727		{
728			var foo = Mock.Create<IFoo>();
729			Mock.Arrange(() => foo.Echo(0, 0)).Returns(2);
730
731			int ret = foo.Echo(0, 0);
732
733			Assert.Equal(2, ret);
734		}
735
736		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
737		public void ShouldCreateMockFromRealObject()
738		{
739			var realItem = Mock.Create(() => new RealItem());
740			Assert.NotNull(realItem);
741		}
742
743
744		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
745		public void ShouldCreateMockFromRealObjectForNonDefaultConstructor()
746		{
747			var realItem = Mock.Create(() => new RealItem(10));
748			Assert.NotNull(realItem);
749		}
750
751		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
752		public void ShouldCreateMockFromRealObjectThatHasOnlyNonDefaultCtor()
753		{
754			var realItem = Mock.Create(() => new RealItem2(10, string.Empty));
755			Assert.NotNull(realItem);
756		}
757
758		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
759		public void ShouldCreateMockFromRealCtorWithParams()
760		{
761			// the following line should not throw any argument exception.
762			var realItem = Mock.Create(() => new RealItem("hello", 10, 20),
763				Behavior.CallOriginal);
764
765			Assert.Equal("hello", realItem.Text);
766			Assert.Equal(2, realItem.Args.Length);
767		}
768
769		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
770		public void ShouldAssertMixins()
771		{
772			var realItem = Mock.Create<RealItem>(x =>
773			{
774				x.Implements<IDisposable>();
775				x.CallConstructor(() => new RealItem(0));
776			});
777			var iDispose = realItem as IDisposable;
778
779			iDispose.Dispose();
780		}
781
782		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
783		public void ShouldAssertMixinsWithClosure()
784		{
785			int a = 5;
786			var realItem = Mock.Create<RealItem>(x =>
787			{
788				x.Implements<IDisposable>();
789				x.CallConstructor(() => new RealItem(a));
790			});
791			var iDispose = realItem as IDisposable;
792
793			iDispose.Dispose();
794		}
795
796		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
797		public void ShouldImplementDependentInterfacesWhenTopIsSpecified()
798		{
799			var realItem = Mock.Create<RealItem>(x =>
800			{
801				x.Implements<IFooImplemted>();
802				x.CallConstructor(() => new RealItem(0));
803			});
804
805			Assert.NotNull(realItem as IFoo);
806		}
807
808		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
809		public void ShouldCloneWhenItemImplementsICloneableAndOneOtherInterface()
810		{
811			var myMock = Mock.Create<IDisposable>(x => x.Implements<ICloneable>());
812			var myMockAsClonable = myMock as ICloneable;
813			bool isCloned = false;
814
815			Mock.Arrange(() => myMockAsClonable.Clone()).DoInstead(() => isCloned = true);
816
817			myMockAsClonable.Clone();
818
819			Assert.True(isCloned);
820		}
821
822		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
823		public void ShouldCallOriginalMethodForCallsOriginal()
824		{
825			var foo = Mock.Create<FooBase>(Behavior.CallOriginal);
826			//// should call the original.
827			Assert.Throws<InvalidOperationException>(() => foo.ThrowException());
828		}
829
830		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
831		public void ShouldNotCallOriginalMethodIfThereisASetupForCallsOriginal()
832		{
833			var foo = Mock.Create<FooBase>(Behavior.CallOriginal);
834
835			Guid expected = Guid.NewGuid();
836
837			Mock.Arrange(() => foo.GetGuid()).Returns(expected);
838
839			Assert.Equal(expected, foo.GetGuid());
840		}
841
842		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
843		public void ShouldCallOriginalIfThereIsNoSetupForSimilarTypeForCallsOiginal()
844		{
845			var foo = Mock.Create<FooBase>(Behavior.CallOriginal);
846
847			Mock.Arrange(() => foo.Echo(1)).Returns(2);
848
849			Assert.Equal(2, foo.Echo(1));
850			Assert.Equal(2, foo.Echo(2));
851		}
852
853		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
854		public void ShouldBeAbleToIgnoreArgumentsIfSpecified()
855		{
856			var foo = Mock.Create<IFoo>();
857
858			Mock.Arrange(() => foo.Echo(10, 9)).IgnoreArguments().Returns(11);
859
860			Assert.Equal(11, foo.Echo(1, 1));
861		}
862
863		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
864		public void ShouldInterceptVirtualsFromBaseClass()
865		{
866			var foo = Mock.Create<FooChild>();
867
868			Mock.Arrange(() => foo.ThrowException()).Throws<ArgumentException>();
869
870			Assert.Throws<ArgumentException>(() => foo.ThrowException());
871		}
872
873		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
874		public void ShouldAssertSetupWithArgAnyMatcherForArray()
875		{
876			var foo = Mock.Create<IFoo>();
877
878			Mock.Arrange(() => foo.Submit(Arg.IsAny<byte[]>())).MustBeCalled();
879
880			foo.Submit(new byte[10]);
881
882			Mock.Assert(foo);
883		}
884
885		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
886		public void ShouldAssertDictionaryArgumentForIsAny()
887		{
888			var param = Mock.Create<IParams>();
889
890			Mock.Arrange(() =>
891				param.ExecuteArrayWithString(Arg.AnyString, Arg.IsAny<Dictionary<string, object>>()))
892				.MustBeCalled();
893
894			param.ExecuteArrayWithString("xxx", new Dictionary<string, object>());
895
896			Mock.Assert(param);
897		}
898
899		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
900		public void ShouldAssertSetupWithCallHavingParamsAndPassedWithMatcher()
901		{
902			var foo = Mock.Create<IFoo>();
903
904			Mock.Arrange(() => foo.FindOne(Arg.IsAny<ICriteria>())).Returns(true);
905
906			var criteria = Mock.Create<ICriteria>();
907
908			Assert.True(foo.FindOne(criteria));
909		}
910
911		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
912		public void ShouldThrowNotImplementedExceptionForBaseInovocationOnAbstract()
913		{
914			var node = Mock.Create<ExpressionNode>(Behavior.CallOriginal);
915			Assert.Throws<NotImplementedException>(() => { var expected = node.NodeType; });
916		}
917
918		[TestMethod, TestCategory("Lite"), TestCategory("Behavior"), TestCategory("CallOriginal")]
919		public void CallOriginalClause_AbstractMethod_ThrowsNotImplemented()
920		{
921			var mock = Mock.Create<IFoo>();
922			Mock.Arrange(() => mock.JustCall()).CallOriginal();
923			Assert.Throws<NotImplementedException>(() => mock.JustCall());
924		}
925
926		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
927		public void ShouldSetupMockWithParamsWhenNoParamIsPassed()
928		{
929			var fooParam = Mock.Create<FooParam>();
930
931			var expected = "hello";
932
933			Mock.Arrange(() => fooParam.FormatWith(expected)).Returns(expected);
934
935			Assert.Equal(expected, fooParam.FormatWith(expected));
936		}
937
938		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
939		public void ShouldBeAbleToPassSingleArgIsAnyForParamsTypeArgument()
940		{
941			var fooParam = Mock.Create<FooParam>();
942
943			Mock.Arrange(() => fooParam.GetDevicesInLocations(0, false, Arg.IsAny<MesssageBox>())).Returns(10);
944
945			int result = fooParam.GetDevicesInLocations(0, false, new MesssageBox());
946
947			Assert.Equal(10, result);
948		}
949
950		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
951		public void ShouldBeAbleToPassMultipleArgIsAnyForParamsTypeArgument()
952		{
953			var fooParam = Mock.Create<FooParam>();
954
955			Mock.Arrange(() => fooParam
956				.GetDevicesInLocations(0, false, Arg.IsAny<MesssageBox>(), Arg.IsAny<MesssageBox>()))
957				.Returns(10);
958
959			var box = new MesssageBox();
960
961			int result = fooParam.GetDevicesInLocations(0, false, box, box);
962
963			Assert.Equal(10, result);
964		}
965
966		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
967		public void ShouldCreateMockFromInterfaceWithSimilarGenericOverloads()
968		{
969			var session = Mock.Create<ISession>();
970			Assert.NotNull(session);
971		}
972
973		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
974		public void ShouldDisposeArrangementEffects()
975		{
976			var mock = Mock.Create<IBar>();
977			using (Mock.Arrange(() => mock.Value).Returns(123))
978			{
979				Assert.Equal(123, mock.Value);
980			}
981			Assert.Equal(0, mock.Value);
982		}
983
984		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
985		public void ShouldDisposeArrangementExpectations()
986		{
987			var mock = Mock.Create<IBar>();
988			using (Mock.Arrange(() => mock.Value).MustBeCalled())
989			{
990				Assert.Throws<AssertionException>(() => Mock.Assert(mock));
991			}
992			Mock.Assert(mock);
993		}
994
995		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
996		public void ShouldAssertExpectedWithDynamicQuery()
997		{
998			var bookRepo = Mock.Create<IBookRepository>();
999
1000			var expected = new Book();
1001
1002			Mock.Arrange(() => bookRepo.GetWhere(book => book.Id > 1))
1003					.Returns(expected)
1004					.MustBeCalled();
1005
1006			var actual = bookRepo.GetWhere(book => book.Id > 1);
1007
1008			Assert.Equal(expected, actual);
1009
1010			Mock.Assert(bookRepo);
1011		}
1012
1013		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1014		public void ShouldAssertMockExpressionDeleteArgumentForCompoundQuery()
1015		{
1016			var bookRepo = Mock.Create<IBookRepository>();
1017
1018			var expected = new Book();
1019
1020			string expectedTitle = "Telerik";
1021
1022			Mock.Arrange(() => bookRepo.GetWhere(book => book.Id > 1 && book.Title == expectedTitle))
1023					.Returns(expected)
1024					.MustBeCalled();
1025
1026			var actual = bookRepo.GetWhere(book => book.Id > 1 && book.Title == expectedTitle);
1027
1028			Assert.Equal(expected, actual);
1029
1030			Mock.Assert(bookRepo);
1031		}
1032
1033		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1034		public void ShouldAssertMockForDynamicQueryWhenComparedUsingAVariable()
1035		{
1036			var repository = Mock.Create<IBookRepository>();
1037			var expected = new Book { Title = "Adventures" };
1038			var service = new BookService(repository);
1039
1040			Mock.Arrange(() => repository.GetWhere(book => book.Id == 1))
1041				.Returns(expected)
1042				.MustBeCalled();
1043
1044			var actual = service.GetSingleBook(1);
1045
1046			Assert.Equal(actual.Title, expected.Title);
1047		}
1048
1049		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1050		public void ShouldAssertWithIntValueUsedForLongValuedArgument()
1051		{
1052			int number = 5;
1053			int expectedResult = 42;
1054
1055			var myClass = Mock.Create<ClassWithLongMethod>();
1056
1057			Mock.Arrange(() => myClass.AddOne(number)).Returns(expectedResult);
1058
1059			// Act
1060			var result = myClass.AddOne(number);
1061
1062			// Assert
1063			Assert.Equal(expectedResult, result);
1064		}
1065
1066		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1067		public void ShouldAutoArrangePropertySetInConstructor()
1068		{
1069			var expected = "name";
1070			var item = Mock.Create<Item>(() => new Item(expected));
1071
1072			Assert.Equal(expected, item.Name);
1073		}
1074
1075		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1076		public void ShouldTakeOutValueFromDoInteadWhenDefinedWithCustomDelegate()
1077		{
1078			int outArg = 1;
1079			var mock = Mock.Create<DoInsteadWithCustomDelegate>();
1080			Mock.Arrange(() => mock.Do(0, ref outArg)).DoInstead(new RefAction<int, int>((int i, ref int arg2) => { arg2 = 2; }));
1081
1082			mock.Do(0, ref outArg);
1083
1084			Assert.Equal(2, outArg);
1085		}
1086
1087		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1088		public void ShouldCheckMethodOverloadsWhenResolvingInterfaceInheritance()
1089		{
1090			var project = Mock.Create<IProject>();
1091			Assert.NotNull(project);
1092		}
1093
1094		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1095		public void PropertySetShouldThrowExceptionWhenNameHasSet_Literal()
1096		{
1097			var b_object = Mock.Create<B>();
1098
1099			Mock.ArrangeSet<B>(() => b_object.b_string_set_get = string.Empty).DoNothing().MustBeCalled();
1100
1101			b_object.b_string_set_get = string.Empty;
1102
1103			Mock.Assert(b_object);
1104		}
1105
1106		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1107		public void ShouldNotAffectAssertionForInvalidAsserts()
1108		{
1109			var foo = Mock.Create<IFoo>();
1110
1111			Guid goodGuid = Guid.NewGuid();
1112			Guid badGuid = Guid.NewGuid();
1113
1114			Mock.Arrange(() => foo.CallMeOnce(true, goodGuid)).OccursOnce();
1115
1116			foo.CallMeOnce(true, goodGuid);
1117
1118			Mock.Assert(() => foo.CallMeOnce(true, badGuid), Occurs.Never());
1119			Mock.Assert(() => foo.CallMeOnce(true, Guid.Empty), Occurs.Never());
1120
1121			Mock.Assert(() => foo.CallMeOnce(true, goodGuid), Occurs.Once());
1122			Mock.Assert(() => foo.CallMeOnce(false, badGuid), Args.Ignore(), Occurs.Once());
1123
1124			Mock.Assert(() => foo.CallMeOnce(Arg.AnyBool, badGuid), Occurs.Never());
1125			Mock.Assert(() => foo.CallMeOnce(Arg.IsAny<bool>(), badGuid), Occurs.Never());
1126		}
1127
1128		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1129		public void ShouldEqualityCheckForMockFromAnInterfaceThatHasEquals()
1130		{
1131			IRule mockRule1 = Mock.Create<IRule>();
1132
1133			List<IRule> ruleList = new List<IRule>();
1134
1135			Assert.False(ruleList.Contains(mockRule1));
1136
1137			ruleList.Add(mockRule1);
1138
1139			Assert.True(ruleList.Contains(mockRule1));
1140		}
1141
1142		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1143		public void ShouldAssertMethodWithKeyValuePairTypeArgument()
1144		{
1145			var presenter = Mock.Create<InteractiveKioskPresenter>(Behavior.CallOriginal);
1146
1147			var key = Mock.Create<IKioskPart>();
1148			var val = Mock.Create<IKioskWellInfo>();
1149
1150			presenter.ShowControl(new KeyValuePair<IKioskPart, IKioskWellInfo>(key, val));
1151		}
1152
1153		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1154		public void ShouldAssertMethodWithStructTypeArgument()
1155		{
1156			var presenter = Mock.Create<InteractiveKioskPresenter>(Behavior.CallOriginal);
1157
1158			Size size = new Size();
1159
1160			presenter.DrawRect(size);
1161		}
1162
1163		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1164		public void ShouldParsePrimitiveParamsArrayCorrectly()
1165		{
1166			var foo = Mock.Create<IFoo>();
1167
1168			Mock.Arrange(() => foo.SubmitWithParams(Arg.AnyInt)).MustBeCalled();
1169
1170			foo.SubmitWithParams(10);
1171
1172			Mock.Assert(foo);
1173		}
1174
1175		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1176		public void ShouldAssertCorrectMethodWhenDifferentArgumentsPassedForParamSetup()
1177		{
1178			var foo = Mock.Create<IFoo>();
1179
1180			Mock.Arrange(() => foo.SubmitWithParams(10)).OccursOnce();
1181
1182			foo.SubmitWithParams(10);
1183			foo.SubmitWithParams(10, 11);
1184
1185			Mock.Assert(foo);
1186		}
1187
1188		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1189		public void ShouldAssertOccursForIndexedPropertyWithDifferentArguments()
1190		{
1191			var foo = Mock.Create<Foo>();
1192
1193			string expected = "string";
1194
1195			Mock.Arrange(() => foo.Baz["Test"]).Returns(expected);
1196			Mock.Arrange(() => foo.Baz["TestName"]).Returns(expected);
1197
1198			Assert.Equal(expected, foo.Baz["Test"]);
1199			Assert.Equal(expected, foo.Baz["TestName"]);
1200
1201			Mock.Assert(() => foo.Baz["Test"], Occurs.Once());
1202			Mock.Assert(() => foo.Baz["TestName"], Occurs.Once());
1203		}
1204
1205		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1206		public void ShouldNotSkipBaseInterfaceWhenSomeMembersAreSame()
1207		{
1208			var loanString = Mock.Create<ILoanStringField>();
1209			Assert.NotNull(loanString);
1210		}
1211
1212		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1213		public void ShouldAssertParamsArrayAsArrayBasedOnArgument()
1214		{
1215			string value1 = "Hello";
1216			string value2 = "World";
1217
1218			var session = Mock.Create<IMockable>();
1219
1220			Mock.Arrange(() => session.Get<string>(Arg.Matches<string[]>(v => v.Contains("Lol") &&
1221													 v.Contains("cakes"))))
1222				.Returns(new[]
1223				 {
1224					 value1,
1225					 value2,
1226				 });
1227
1228			var testValues = new[]{
1229						 "Lol",
1230						 "cakes"
1231					 };
1232
1233			var result = session.Get<string>(testValues);
1234
1235			Assert.Equal(value1, result[0]);
1236			Assert.Equal(value2, result[1]);
1237		}
1238
1239		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1240		public void ShouldNotInitRescursiveMockingWithProfilerForProperyThatReturnsMock()
1241		{
1242			WorkerHelper helper = new WorkerHelper();
1243
1244			helper.Arrange();
1245
1246			helper.Worker.Echo("hello");
1247		}
1248
1249		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1250		public void ShouldAssertMockWithEnumArgumentWithUnderlyingTypeOtherThanInt()
1251		{
1252			var subdivisionTypeCode = SubdivisionTypeCode.City;
1253			var subdivisionTypeRepository = Mock.Create<ISubdivisionTypeRepository>();
1254
1255			Mock.Arrange(() => subdivisionTypeRepository.Get(subdivisionTypeCode)).Returns((SubdivisionTypeCode subDivision) =>
1256			{
1257				return subDivision.ToString();
1258			});
1259
1260			var result = subdivisionTypeRepository.Get(subdivisionTypeCode);
1261
1262			Assert.Equal(result, subdivisionTypeCode.ToString());
1263
1264			Mock.AssertAll(subdivisionTypeRepository);
1265		}
1266
1267		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1268		public void ShouldAssertMockWithNullableValueTypeArg()
1269		{
1270			FooNullable foo = Mock.Create<FooNullable>();
1271
1272			var now = DateTime.Now;
1273
1274			Mock.Arrange(() => foo.ValideDate(now)).MustBeCalled();
1275
1276			foo.ValideDate(now);
1277
1278			Mock.Assert(foo);
1279		}
1280
1281		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1282		public void ShouldAssertMockWithNullForNullableValueTypeArg()
1283		{
1284			FooNullable foo = Mock.Create<FooNullable>();
1285
1286			Mock.Arrange(() => foo.ValideDate(null)).MustBeCalled();
1287
1288			foo.ValideDate(null);
1289
1290			Mock.Assert(foo);
1291		}
1292
1293		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1294		public void ShouldAssertCallOriginalForAbstractClass()
1295		{
1296			Assert.NotNull(Mock.Create<TestTreeItem>(Behavior.CallOriginal));
1297		}
1298
1299		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1300		public void ShouldCallBaseWhenCallOriginalSpecifiedForMock()
1301		{
1302			var item = Mock.Create<TestTreeItem>(Behavior.CallOriginal);
1303			var result = ((IComparable)item).CompareTo(10);
1304
1305			Assert.Equal(1, result);
1306		}
1307
1308		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1309		public void ShouldArrangeBothInterfaceMethodAndImplementation()
1310		{
1311			var mock = Mock.Create<FrameworkElement>() as ISupportInitialize;
1312
1313			bool implCalled = false;
1314			Mock.Arrange(() => mock.Initialize()).DoInstead(() => implCalled = true).MustBeCalled();
1315
1316			mock.Initialize();
1317			Assert.True(implCalled);
1318
1319			Mock.Assert(() => mock.Initialize());
1320		}
1321
1322		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1323		public void ShouldArrangeBothBaseAndOverriddenMethod()
1324		{
1325			var mock = Mock.Create<Control>() as FrameworkElement;
1326
1327			bool implCalled = false;
1328			Mock.Arrange(() => mock.Initialize()).DoInstead(() => implCalled = true);
1329
1330			mock.Initialize();
1331			Assert.True(implCalled);
1332
1333			Mock.Assert(() => mock.Initialize());
1334			Mock.Assert(() => ((ISupportInitialize)mock).Initialize());
1335		}
1336
1337		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1338		public void ShouldArrangeBaseMethodInManyImplementations()
1339		{
1340			var fe = Mock.Create<FrameworkElement>();
1341			var control = Mock.Create<Control>();
1342
1343			int calls = 0;
1344			Mock.Arrange(() => (null as ISupportInitialize).Initialize()).DoInstead(() => calls++);
1345
1346			fe.Initialize();
1347			Assert.Equal(1, calls);
1348
1349			control.Initialize();
1350			Assert.Equal(2, calls);
1351		}
1352
1353		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1354		public void ShouldAssertMethodAtAllHierarchyLevels()
1355		{
1356			var control = Mock.Create<Control>();
1357
1358			control.Initialize();
1359
1360			Mock.Assert(() => control.Initialize(), Occurs.Once());
1361			Mock.Assert(() => (control as FrameworkElement).Initialize(), Occurs.Once());
1362			Mock.Assert(() => (control as ISupportInitialize).Initialize(), Occurs.Once());
1363		}
1364
1365		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1366		public void ShouldArrangeBaseMethodInManyImplementationsForProperty()
1367		{
1368			var fe = Mock.Create<FrameworkElement>();
1369			var control = Mock.Create<Control>();
1370
1371			int calls = 0;
1372			Mock.Arrange(() => (null as ISupportInitialize).Property).DoInstead(() => calls++);
1373
1374			var property = fe.Property;
1375			Assert.Equal(1, calls);
1376
1377			property = control.Property;
1378			Assert.Equal(2, calls);
1379		}
1380
1381		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1382		public void ShouldAssertMethodAtAllHierarchyLevelsForProperty()
1383		{
1384			var control = Mock.Create<Control>();
1385
1386			var property = control.Property;
1387
1388			Mock.Assert(() => control.Property, Occurs.Once());
1389			Mock.Assert(() => (control as FrameworkElement).Property, Occurs.Once());
1390			Mock.Assert(() => (control as ISupportInitialize).Property, Occurs.Once());
1391		}
1392
1393		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1394		public void ShouldArrangeInheritableMemberOfExplicitlySpecifiedType()
1395		{
1396			var ident = Mock.Create<IIdentifiable>();
1397			Mock.Arrange<IIdentifiable, int>(new Func<int>(() => ident.Id)).Returns(15);
1398			Assert.Equal(15, ident.Id);
1399		}
1400
1401#if !SILVERLIGHT
1402		[TestMethod, TestCategory("Elevated"), TestCategory("Lite"), TestCategory("Mock")]
1403		public void ShouldNotCreateProxyIfNotNecessary()
1404		{
1405			var mock = Mock.Create<Poco>();
1406			Mock.Arrange(() => mock.Data).Returns(10);
1407			Assert.Equal(10, mock.Data);
1408			if (Mock.IsProfilerEnabled)
1409				Assert.Same(typeof(Poco), mock.GetType());
1410		}
1411#elif !LITE_EDITION
1412		[TestMethod, TestCategory("Elevated"), TestCategory("Mock")]
1413		public void ShouldNotCreateProxyIfNotNecessary()
1414		{
1415			var mock = Mock.Create<Poco>(Constructor.Mocked);
1416			Mock.Arrange(() => mock.Data).Returns(10);
1417			Assert.Equal(10, mock.Data);
1418			Assert.Same(typeof(Poco), mock.GetType());
1419		}
1420#endif
1421
1422#if LITE_EDITION && !COREFX
1423		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1424		public void MockInternalMembersWithoutExplicitlyGivenVisibilitySentinel()
1425		{
1426			Assert.Throws<MockException>(() => Mock.Create<InvisibleInternal>());
1427		}
1428#endif
1429
1430		public class Poco // should be inheritable but shouldn't be abstract
1431		{
1432			public virtual int Data { get { return 0; } }
1433		}
1434
1435		public interface IIdentifiable
1436		{
1437			int Id { get; }
1438		}
1439
1440		public interface ISupportInitialize
1441		{
1442			void Initialize();
1443			string Property { get; }
1444		}
1445
1446		public abstract class FrameworkElement : ISupportInitialize
1447		{
1448			public abstract void Initialize();
1449			public abstract string Property { get; set; }
1450		}
1451
1452		public abstract class Control : FrameworkElement
1453		{
1454			public override void Initialize()
1455			{
1456				throw new NotImplementedException();
1457			}
1458
1459			public override string Property
1460			{
1461				get
1462				{
1463					throw new NotImplementedException();
1464				}
1465				set
1466				{
1467					throw new NotImplementedException();
1468				}
1469			}
1470		}
1471
1472		public abstract class TestTreeItem : IComparable
1473		{
1474			int IComparable.CompareTo(object obj)
1475			{
1476				return 1;
1477			}
1478		}
1479
1480		public class FooNullable
1481		{
1482			public virtual void ValideDate(DateTime? date)
1483			{
1484
1485			}
1486		}
1487
1488		public enum SubdivisionTypeCode : byte
1489		{
1490			None = 255,
1491			State = 0,
1492			County = 1,
1493			City = 2,
1494		}
1495
1496		public interface ISubdivisionTypeRepository
1497		{
1498			string Get(SubdivisionTypeCode subdivisionTypeCode);
1499		}
1500
1501		public class WorkerHelper
1502		{
1503			public IWorker TheWorker { get; private set; }
1504
1505			public WorkerHelper()
1506			{
1507				this.TheWorker = Mock.Create<IWorker>(Behavior.Strict);
1508			}
1509
1510			public IWorker Worker
1511			{
1512				get
1513				{
1514					return this.TheWorker;
1515				}
1516			}
1517
1518			public void Arrange()
1519			{
1520				Mock.Arrange(() => this.TheWorker.Echo(Arg.AnyString)).DoNothing();
1521			}
1522		}
1523
1524		public interface IWorker
1525		{
1526			void Echo(string value);
1527		}
1528
1529		public interface IMockable
1530		{
1531			T[] Get<T>(params string[] values);
1532		}
1533
1534		public interface ILoanStringField : ILoanField
1535		{
1536			string Value { get; set; }
1537		}
1538
1539		public interface ILoanField
1540		{
1541			void ClearValue();
1542			object Value { get; set; }
1543		}
1544
1545		public interface IKioskPart
1546		{
1547		}
1548
1549		public interface IKioskWellInfo
1550		{
1551		}
1552
1553		public class InteractiveKioskPresenter
1554		{
1555			public virtual void ShowControl(KeyValuePair<IKioskPart, IKioskWellInfo> kPart)
1556			{
1557
1558			}
1559
1560			public virtual void DrawRect(Size size)
1561			{
1562
1563			}
1564
1565		}
1566
1567		public struct Size
1568		{
1569		}
1570
1571		public interface IRule
1572		{
1573			bool Equals(object obj);
1574		}
1575
1576		public class B
1577		{
1578			public string b_string = null;
1579
1580			public virtual string b_string_set_get { get { return b_string; } set { b_string = value; } }
1581		}
1582
1583		public interface IProject : IProjectItemContainer
1584		{
1585			IEnumerable<IProjectItem> Items { get; }
1586			void AddChild();
1587		}
1588
1589		public interface IProjectItem : IProjectItemContainer
1590		{
1591		}
1592
1593		public interface IProjectItemContainer : IDocumentItemContainer<IProjectItem>
1594		{
1595			bool CanAddChild { get; }
1596		}
1597
1598		public interface IDocumentItemContainer : IDocumentItem
1599		{
1600			IEnumerable<IDocumentItem> Children { get; }
1601		}
1602
1603		public interface IDocumentItemContainer<T> : IDocumentItemContainer
1604		where T : IDocumentItem
1605		{
1606			IEnumerable<T> Children { get; }
1607
1608			void AddChild(T child);
1609			void RemoveChild(T child);
1610		}
1611
1612		public interface IDocumentItem
1613		{
1614		}
1615
1616		public delegate void RefAction<T1, T2>(T1 arg1, ref T2 arg2);
1617
1618		public class DoInsteadWithCustomDelegate
1619		{
1620			public virtual void Do(int k, ref int j)
1621			{
1622
1623			}
1624		}
1625
1626		public class ClassWithLongMethod
1627		{
1628			public virtual long AddOne(long number)
1629			{
1630				return number + 1;
1631			}
1632		}
1633
1634		public class BookService
1635		{
1636			private IBookRepository repository;
1637
1638			public BookService(IBookRepository repository)
1639			{
1640				this.repository = repository;
1641			}
1642
1643			public Book GetSingleBook(int id)
1644			{
1645				return repository.GetWhere(book => book.Id == id);
1646			}
1647		}
1648
1649		public interface IBookRepository
1650		{
1651			Book GetWhere(Expression<Func<Book, bool>> expression);
1652		}
1653
1654		public class Book
1655		{
1656			public int Id { get; private set; }
1657			public string Title { get; set; }
1658		}
1659
1660
1661		#region Syntax Integrity
1662
1663		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1664		public void ShouldBeAbleToInvokeMustBeCalledWithIgnoreArguments()
1665		{
1666			var foo = Mock.Create<Foo>();
1667
1668			Mock.Arrange(() => foo.Execute(0)).IgnoreArguments().MustBeCalled();
1669
1670			foo.Execute(10);
1671
1672			Mock.Assert(foo);
1673		}
1674
1675		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1676		public void ShouldBeAbleToUseMuseBeCalledAfterIgnoreFoFunc()
1677		{
1678			var foo = Mock.Create<Foo>();
1679
1680			Mock.Arrange(() => foo.Echo(0)).IgnoreArguments().MustBeCalled();
1681
1682			foo.Echo(10);
1683
1684			Mock.Assert(foo);
1685		}
1686
1687		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1688		public void ShouldBeAbleToDoDoNothingForNonVoidCalls()
1689		{
1690			var foo = Mock.Create<Foo>();
1691			Mock.Arrange(() => foo.Echo(Arg.AnyInt)).DoNothing();
1692			foo.Echo(10);
1693		}
1694
1695		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1696		public void ShouldBeAbleToSpecifyOccursAfterReturns()
1697		{
1698			var foo = Mock.Create<Foo>();
1699
1700			Mock.Arrange(() => foo.Echo(Arg.AnyInt))
1701				.Returns(10)
1702				.Occurs(1);
1703
1704			foo.Echo(10);
1705
1706			Mock.Assert(foo);
1707		}
1708
1709		internal abstract class FooAbstract
1710		{
1711			protected internal abstract bool TryCreateToken(string literal);
1712		}
1713
1714		internal abstract class FooAbstract2 : FooAbstract
1715		{
1716
1717		}
1718
1719		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1720		public void ShouldAsssertMockHavingInternalAbstractBaseMethod()
1721		{
1722			var foo = Mock.Create<FooAbstract2>();
1723			foo.TryCreateToken(string.Empty);
1724		}
1725
1726		#endregion
1727
1728		public interface ISession
1729		{
1730			ICriteria CreateCriteria<T>() where T : class;
1731			ICriteria CreateCriteria(string entityName);
1732			ICriteria CreateCriteria<T>(string alias) where T : class;
1733			ICriteria CreateCriteria(System.Type persistentClass);
1734			ICriteria CreateCriteria(string entityName, string alias);
1735			ICriteria CreateCriteria(System.Type persistentClass, string alias);
1736		}
1737
1738		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
1739		public void ShouldNotTryToWeaveMethodInSilverlightRuntime()
1740		{
1741			var foo = Mock.Create<FooSilver>();
1742			Assert.NotNull(foo);
1743		}
1744
1745		public interface ICloneable
1746		{
1747			object Clone();
1748		}
1749
1750		public abstract class FooSilver
1751		{
1752			public void Do()
1753			{
1754
1755			}
1756		}
1757
1758#if LITE_EDITION
1759		[TestMethod]
1760		public void ShouldThrowMockExceptionWhenTryingToMockFinalMethod()
1761		{
1762			var mock = Mock.Create<Bar>();
1763			Assert.Throws<MockException>(() => Mock.Arrange(() => mock.Submit()).DoNothing());
1764		}
1765
1766		public class Bar
1767		{
1768			public void Submit()
1769			{
1770			}
1771		}
1772#endif
1773
1774		#region Test Insfrastructure
1775
1776		public class Foo
1777		{
1778			public virtual void Execute(int arg1)
1779			{
1780			}
1781
1782			public virtual int Echo(int arg1)
1783			{
1784				return arg1;
1785			}
1786
1787			public virtual Baz Baz { get; set; }
1788		}
1789
1790		public class Baz
1791		{
1792			public virtual string this[string key]
1793			{
1794				get
1795				{
1796					return null;
1797				}
1798			}
1799		}
1800
1801		public class FooParam
1802		{
1803			public virtual int GetDevicesInLocations(int arg1, bool bExclude, params MesssageBox[] box)
1804			{
1805				return default(int);
1806			}
1807
1808			public virtual string FormatWith(string format, params object[] args)
1809			{
1810				return string.Empty;
1811			}
1812		}
1813
1814		public class MesssageBox
1815		{
1816		}
1817
1818		public enum ExpressionNodeType
1819		{
1820			Constant,
1821			Binary
1822		}
1823
1824		public abstract class ExpressionNode
1825		{
1826			public abstract ExpressionNodeType NodeType { get; }
1827		}
1828
1829		public class RealItem
1830		{
1831			public RealItem()
1832			{
1833
1834			}
1835
1836			public RealItem(int num)
1837			{
1838
1839			}
1840
1841			public RealItem(string text, params int[] args)
1842			{
1843				if (args.Length == 0 || string.IsNullOrEmpty(text))
1844				{
1845					throw new ArgumentException();
1846				}
1847
1848				this.text = text;
1849				this.args = args;
1850			}
1851
1852			public string Text
1853			{
1854				get
1855				{
1856					return text;
1857				}
1858			}
1859
1860			public int[] Args
1861			{
1862				get
1863				{
1864					return args;
1865				}
1866			}
1867
1868			public string text;
1869			private int[] args;
1870		}
1871
1872		public class RealItem2
1873		{
1874			public RealItem2(int num, string str)
1875			{
1876			}
1877		}
1878
1879		public class Item
1880		{
1881			public virtual string Name { get; set; }
1882
1883			public Item(string name)
1884			{
1885				Name = name;
1886			}
1887		}
1888
1889		public class FooGeneric<T>
1890		{
1891			public virtual T Get<T1, T2>(T1 p1, T2 p2)
1892			{
1893				return default(T);
1894			}
1895			public virtual void Execute<T1>(T1 arg)
1896			{
1897				throw new Exception();
1898			}
1899		}
1900
1901		public class FooGeneric
1902		{
1903			public virtual TRet Get<T, TRet>(T arg1)
1904			{
1905				return default(TRet);
1906			}
1907
1908			public virtual TRet Execute<T1, TRet>(out T1 arg1)
1909			{
1910				arg1 = default(T1);
1911
1912				object[] args = new object[1];
1913				args[0] = arg1;
1914
1915				return default(TRet);
1916			}
1917
1918			public virtual int Get<T1>()
1919			{
1920				throw new NotImplementedException();
1921			}
1922		}
1923
1924		public class FooWithInternalConstruct
1925		{
1926			internal FooWithInternalConstruct()
1927			{
1928
1929			}
1930
1931			public virtual void Execute()
1932			{
1933				throw new ArgumentException();
1934			}
1935		}
1936
1937		public class Nested
1938		{
1939			public int expected;
1940			public int expeted1;
1941		}
1942		class FooService : IFooService { }
1943		interface IFooService { }
1944
1945		public interface ICriteria
1946		{
1947
1948		}
1949
1950		public interface IFoo
1951		{
1952			string Execute(string arg);
1953			void JustCall();
1954			void Execute(Guid guid);
1955			void Execute(bool flag, Guid guid);
1956			void Execute(out int expected);
1957			void Execute(out DateTime date);
1958			IFoo GetFoo();
1959
1960			int Echo(int arg1);
1961			int Echo(int arg1, int arg2);
1962			int Echo(int arg1, int arg2, int arg3);
1963			int Echo(int arg1, int arg2, int arg3, int arg4);
1964
1965			void Submit(int arg1);
1966			void Submit(int arg1, int arg2);
1967			void Submit(int arg1, int arg2, int arg3);
1968			void Submit(int arg1, int arg2, int arg3, int arg4);
1969			void Submit(byte[] arg);
1970
1971			void SubmitWithParams(params int[] args);
1972
1973			void CallMeOnce(bool flag, Guid guid);
1974
1975			bool FindOne(params ICriteria[] criteria);
1976		}
1977
1978
1979		public interface IBar
1980		{
1981			int Echo(int value);
1982			int Value { get; set; }
1983		}
1984
1985		public interface IFooImplemted : IFoo
1986		{
1987
1988		}
1989
1990		public class CustomExepction : Exception
1991		{
1992			public CustomExepction(string message, bool throwed)
1993				: base(message)
1994			{
1995
1996			}
1997		}
1998
1999		public class Log
2000		{
2001			public virtual void Info(string message)
2002			{
2003				throw new Exception(message);
2004			}
2005		}
2006
2007		public abstract class FooBase
2008		{
2009			public virtual string GetString(string inString)
2010			{
2011				return inString;
2012			}
2013			public virtual Guid GetGuid()
2014			{
2015				return default(Guid);
2016			}
2017
2018			public virtual int Echo(int arg1)
2019			{
2020				return arg1;
2021			}
2022
2023			public virtual void ThrowException()
2024			{
2025				throw new InvalidOperationException("This should throw expection.");
2026			}
2027		}
2028
2029		public class FooChild : FooBase
2030		{
2031
2032		}
2033
2034		public class FooOverridesEquals
2035		{
2036			public FooOverridesEquals(string name)
2037			{
2038				this.name = name;
2039			}
2040
2041			public override bool Equals(object obj)
2042			{
2043				return (obj is FooOverridesEquals) &&
2044					((FooOverridesEquals)obj).name == this.name;
2045			}
2046
2047			public override int GetHashCode()
2048			{
2049				if (!string.IsNullOrEmpty(name))
2050				{
2051					return name.GetHashCode();
2052				}
2053
2054				return base.GetHashCode();
2055			}
2056
2057			private string name;
2058		}
2059
2060		public interface IParams
2061		{
2062			string ExecuteByName(int index, params string[] args);
2063			string ExecuteParams(params string[] args);
2064			string ExecuteArray(string[] args);
2065			string ExecuteArrayWithString(string arg1, Dictionary<string, object> dic);
2066		}
2067
2068		public class ClassNonDefaultGuidConstructor
2069		{
2070			public ClassNonDefaultGuidConstructor(Guid guidValue)
2071			{
2072				this.guidValue = guidValue;
2073			}
2074
2075			public Guid guidValue;
2076		}
2077
2078		public class ClassWithNonDefaultConstructor
2079		{
2080			internal ClassWithNonDefaultConstructor()
2081			{
2082
2083			}
2084
2085			public ClassWithNonDefaultConstructor(string strValue, int intValue)
2086			{
2087				this.strValue = strValue;
2088				this.intValue = intValue;
2089			}
2090			public ClassWithNonDefaultConstructor(string strValue, int intValue, bool boolValue)
2091			{
2092				this.strValue = strValue;
2093				this.intValue = intValue;
2094				this.boolValue = boolValue;
2095			}
2096
2097			public override string ToString()
2098			{
2099				return string.Format("{0}+{1}+{2}", strValue, intValue, boolValue);
2100			}
2101
2102			private string strValue;
2103			private int intValue;
2104			private bool boolValue;
2105		}
2106
2107		#endregion
2108
2109		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2110		public void ShouldInterpretNullAsDefaultInitializedValueTypeInCtorCall()
2111		{
2112			var mock = Mock.Create<ValueTypeInCtor>(null, null);
2113			Assert.Equal(0, mock.a);
2114		}
2115
2116		public class ValueTypeInCtor
2117		{
2118			public readonly int a;
2119
2120			public ValueTypeInCtor(ValueTypeInCtor q, int a)
2121			{
2122				this.a = a;
2123			}
2124		}
2125
2126#if !__IOS__
2127		[ComImport]
2128#endif
2129		[Guid("4256871F-E8D7-40C2-9E1E-61CFA78C3EC1")]
2130		public interface IVersioned
2131		{
2132			[DispId(1)]
2133			string Identity { [DispId(1)] get; [DispId(1)] set; }
2134		}
2135
2136#if !__IOS__
2137		[ComImport]
2138#endif
2139		[Guid("8DAF6396-300A-46E2-AA4C-CCB6103FB955")]
2140		public interface IVersioned2 : IVersioned
2141		{
2142			[DispId(1)]
2143			string Identity { [DispId(1)] get; [DispId(1)] set; }
2144		}
2145
2146		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2147		public void ShouldCoalesceDifferentMembersWithSameDispIdInVersionedInterfaces()
2148		{
2149			var mock = Mock.Create<IVersioned2>();
2150
2151			mock.Identity = "id";
2152			var baseIdentity = ((IVersioned)mock).Identity;
2153
2154			Assert.Equal("id", baseIdentity);
2155			Assert.Equal("id", mock.Identity);
2156		}
2157
2158		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2159		public void ShouldMockInternalAbstract()
2160		{
2161			var mock = Mock.Create<InternalAbstract>();
2162			Assert.NotNull(mock);
2163		}
2164
2165		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2166		public void ShouldAcceptTypedEnumAsMethodArgument()
2167		{
2168			var mock = Mock.Create<IUnmanagedService>();
2169			Mock.Arrange(() => mock.IsAllowed(ShortFlags.One)).Returns(true);
2170
2171			Assert.True(mock.IsAllowed(ShortFlags.One));
2172			Assert.False(mock.IsAllowed(ShortFlags.None));
2173			Assert.False(mock.IsAllowed(ShortFlags.Two));
2174		}
2175
2176		public enum ShortFlags : short
2177		{
2178			None = 0, One = 1, Two = 100
2179		}
2180
2181		public interface IUnmanagedService
2182		{
2183			bool IsAllowed(ShortFlags flags);
2184		}
2185
2186#if !DOTNET35 && !SILVERLIGHT && !WINDOWS_PHONE
2187		[TestMethod, TestCategory("Lite"), TestCategory("Regression")]
2188		public void ShouldInterceptDynamicProxyMethodsFromMultipleThreads()
2189		{
2190			var generator = Mock.Create<IGuidGenerator>();
2191			var lotsOfGuids = Enumerable.Range(0, 3000)
2192								.AsParallel()
2193								.Select(x => generator.Generate())
2194								.ToArray();
2195
2196			// didn't throw
2197		}
2198
2199		public interface IGuidGenerator
2200		{
2201			Guid Generate();
2202		}
2203#endif
2204
2205		public class ClassWithCtor
2206		{
2207			public ClassWithCtor(string s)
2208			{
2209			}
2210		}
2211
2212		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2213		public void ShouldThrowMeaningfulExceptionWhenConstructorArgumentsAreIncorrect()
2214		{
2215			var ex = Assert.Throws<Exception>(() => Mock.Create<ClassWithCtor>(5));
2216		}
2217
2218		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2219		public void ShouldArrangeMemberFromAdditionalInterfaceOnClassMock()
2220		{
2221			var mock = Mock.Create<Exception>(cfg => cfg.Implements<IIdentity>());
2222			var identity = mock as IIdentity;
2223			Mock.Arrange(() => identity.Name).Returns("mock");
2224			Assert.Equal("mock", identity.Name);
2225		}
2226
2227		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2228		public void ShouldArrangeMemberFromAdditionalInterfaceOnInterfaceMock()
2229		{
2230			var mock = Mock.Create<IPrincipal>(cfg => cfg.Implements<IIdentity>());
2231			var identity = mock as IIdentity;
2232			Mock.Arrange(() => identity.Name).Returns("mock");
2233			Assert.Equal("mock", identity.Name);
2234		}
2235
2236#if !PORTABLE
2237		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2238		public void ShouldMockTypesFromReflectionNamespace()
2239		{
2240			var reflectionTypes = new[]
2241			{
2242				typeof(MemberInfo),
2243				typeof(MethodBase),
2244				typeof(MethodInfo),
2245				typeof(ConstructorInfo),
2246				typeof(FieldInfo),
2247				typeof(PropertyInfo),
2248				typeof(EventInfo),
2249			};
2250
2251			foreach (var type in reflectionTypes)
2252			{
2253				var mock = Mock.Create(type) as MemberInfo;
2254				Mock.Arrange(() => mock.Name).Returns("name");
2255				Assert.Equal("name", mock.Name);
2256			}
2257		}
2258#endif
2259
2260#if !SILVERLIGHT && !WINDOWS_PHONE
2261		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2262		public void ShouldMockWeakReference()
2263		{
2264			var weak = Mock.Create<WeakReference>();
2265			Mock.Arrange(() => weak.IsAlive).Returns(true);
2266			Assert.True(weak.IsAlive);
2267		}
2268#endif
2269
2270		public class CtorWithDefaults
2271		{
2272			public readonly int A;
2273
2274			public CtorWithDefaults(int a = 5)
2275			{
2276				this.A = a;
2277			}
2278		}
2279
2280		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2281		public void ShouldCallConstructorWithDefaultArguments()
2282		{
2283			var mock = Mock.Create<CtorWithDefaults>(Behavior.CallOriginal);
2284			Assert.Equal(5, mock.A);
2285		}
2286
2287#if !PORTABLE
2288		public interface ITwoFace
2289		{
2290			int GetFace1();
2291			int GetFace2();
2292		}
2293
2294		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2295		public void ShouldFilterInterceptors()
2296		{
2297			var mock = Mock.Create<ITwoFace>(conf =>
2298			{
2299				conf.SetInterceptorFilter(mi => mi.Name == "GetFace1");
2300			});
2301
2302			Mock.Arrange(() => mock.GetFace1()).Returns(10);
2303			Mock.Arrange(() => mock.GetFace2()).Returns(20); // TODO: this should actually throw an exception, instead
2304
2305			Assert.Equal(10, mock.GetFace1());
2306			Assert.Equal(0, mock.GetFace2());
2307		}
2308#endif
2309
2310		public class StaticCtor
2311		{
2312			public static bool called;
2313
2314			static StaticCtor()
2315			{
2316				called = true;
2317			}
2318		}
2319
2320		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2321		public void ShouldCallStaticConstructorWhenCreatingMock()
2322		{
2323			var mock = Mock.Create<StaticCtor>();
2324			Assert.True(StaticCtor.called);
2325		}
2326
2327		public interface IGeneric
2328		{
2329			string Get<TItem1, TItem2>(TItem1 a, TItem2 b);
2330		}
2331
2332		public class Generic : IGeneric
2333		{
2334			public virtual string Get<T, U>(T t, U u)
2335			{
2336				return "";
2337			}
2338		}
2339
2340		[TestMethod]
2341		public void ShouldCreateMockWithRenamedGenericParameters()
2342		{
2343			var mock = Mock.Create<Generic>();
2344			Mock.Arrange(() => mock.Get<string, int>("5", 5)).Returns("string");
2345			Assert.Equal("string", mock.Get<string, int>("5", 5));
2346		}
2347
2348		public class SealedGeneric : IGeneric
2349		{
2350			public string Get<TItem1, TItem2>(TItem1 a, TItem2 b)
2351			{
2352				throw new NotImplementedException();
2353			}
2354		}
2355
2356		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2357		public void ShouldArrangeFinalGenericMethodThroughInterface()
2358		{
2359			IGeneric mock = Mock.Create<SealedGeneric>(
2360#if LITE_EDITION
2361			cfg => cfg.Implements<IGeneric>()
2362#endif
2363);
2364
2365			Mock.Arrange(() => mock.Get(5, "4")).Returns("123");
2366			Assert.Equal("123", mock.Get(5, "4"));
2367		}
2368
2369		public interface IEntity
2370		{
2371			string Name { get; set; }
2372		}
2373
2374		public class EntityBase : IEntity
2375		{
2376			public string Name { get; set; }
2377		}
2378
2379		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2380		public void ShouldTreatInterfaceAndImplementationMemberIdentically()
2381		{
2382			var mock = Mock.Create<EntityBase>();
2383			mock.Name = "Git";
2384			var staticName = mock.Name;
2385			var iName = ((IEntity)mock).Name;
2386			Assert.Equal(staticName, iName);
2387		}
2388
2389#if LITE_EDITION && !COREFX
2390		[TestMethod, TestCategory("Lite"), TestCategory("Mock")]
2391		public void ShouldMockNoninheritableInterfaceMembers()
2392		{
2393			var mock = Mock.Create<PrivateInterface>(cfg =>
2394				{
2395					cfg.SetBehavior(Behavior.CallOriginal);
2396					cfg.Implements<IJustDoIt>();
2397					cfg.Implements<IJustDoThat>();
2398					cfg.Implements<Scope.IImplementable>();
2399					cfg.Implements<Scope.IImplementable2>();
2400				});
2401
2402			Assert.Throws<InvalidOperationException>(() => ((IJustDoThat)mock).DoThat());
2403			Mock.Arrange(() => mock.DoThat()).DoNothing();
2404			((IJustDoThat)mock).DoThat();
2405
2406			Assert.Throws<InvalidOperationException>(() => ((IJustDoIt)mock).JustDoIt());
2407			Mock.Arrange(() => ((IJustDoIt)mock).JustDoIt()).DoNothing();
2408			((IJustDoIt)mock).JustDoIt();
2409
2410			Assert.Throws<InvalidOperationException>(() => ((Scope.IImplementable)mock).Do());
2411			Mock.Arrange(() => ((Scope.IImplementable)mock).Do()).DoNothing();
2412			((Scope.IImplementable)mock).Do();
2413
2414			Assert.Throws<InvalidOperationException>(() => ((Scope.IImplementable2)mock).Do());
2415			Mock.Arrange(() => ((Scope.IImplementable2)mock).Do()).DoNothing();
2416			((Scope.IImplementable2)mock).Do();
2417
2418			Assert.Throws<ElevatedMockingException>(() => Mock.Arrange(() => ((INonImplementable)mock).Do()).DoNothing());
2419			Assert.Throws<InvalidOperationException>(() => ((INonImplementable)mock).Do());
2420		}
2421
2422		public interface IJustDoIt
2423		{
2424			void JustDoIt();
2425		}
2426
2427		public interface IJustDoThat
2428		{
2429			void DoThat();
2430		}
2431
2432		private interface INonImplementable
2433		{
2434			void Do();
2435		}
2436
2437		internal class Scope
2438		{
2439			public interface IImplementable
2440			{
2441				void Do();
2442			}
2443
2444			protected internal interface IImplementable2
2445			{
2446				void Do();
2447			}
2448		}
2449
2450		public class PrivateInterface : IJustDoIt, IJustDoThat, INonImplementable, Scope.IImplementable, Scope.IImplementable2
2451		{
2452			void IJustDoIt.JustDoIt()
2453			{
2454				throw new InvalidOperationException();
2455			}
2456
2457			public void DoThat()
2458			{
2459				throw new InvalidOperationException();
2460			}
2461
2462			void INonImplementable.Do()
2463			{
2464				throw new InvalidOperationException();
2465			}
2466
2467			void Scope.IImplementable.Do()
2468			{
2469				throw new InvalidOperationException();
2470			}
2471
2472			void Scope.IImplementable2.Do()
2473			{
2474				throw new InvalidOperationException();
2475			}
2476		}
2477#endif
2478
2479		internal abstract class InternalAbstract
2480		{
2481			internal abstract string Bar { get; set; }
2482		}
2483
2484		#region Category "FluentConfig"
2485
2486		public class Base
2487		{
2488			public const int DefaultValue = 3;
2489
2490			public int i = DefaultValue;
2491
2492			public Base(int i)
2493			{
2494				this.i = i;
2495			}
2496		}
2497
2498		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2499		public void ShouldUseAutoselectedConstructorMockingBehaviorWithFluentGenericConfig()
2500		{
2501			var proxy = Mock.Create<Base>(fluentConfig => { });
2502
2503			Assert.Equal(default(int), proxy.i);
2504			Assert.Null(proxy as IDisposable);
2505		}
2506
2507		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2508		public void ShouldMockWhenMissingPameterlessConstructorAndRecursiveLooseWithFluentGenericConfig()
2509		{
2510			var proxy = Mock.Create<Base>(fluentConfig =>
2511				fluentConfig.SetBehavior(Behavior.RecursiveLoose)
2512			);
2513
2514			Assert.Equal(default(int), proxy.i);
2515			Assert.Null(proxy as IDisposable);
2516		}
2517
2518		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2519		public void ShouldMockWhenMissingPameterlessConstructorAndLooseWithFluentGenericConfig()
2520		{
2521			var proxy = Mock.Create<Base>(fluentConfig =>
2522				fluentConfig.SetBehavior(Behavior.Loose)
2523			);
2524
2525			Assert.Equal(default(int), proxy.i);
2526			Assert.Null(proxy as IDisposable);
2527		}
2528
2529		// Implementation differs for .NETFramework and .NETCore, see DynamicProxyMockFactory.Create method
2530#if !NETCORE
2531		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2532		public void ShouldThrowWhenMissingPameterlessConstructorAndCallOriginalWithFluentGenericConfig()
2533		{
2534			Assert.Throws<MockException>(() =>
2535				Mock.Create<Base>(fluentConfig =>
2536					fluentConfig.SetBehavior(Behavior.CallOriginal))
2537				);
2538		}
2539#else
2540		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2541		public void ShouldMockWhenMissingPameterlessConstructorAndCallOriginalWithFluentGenericConfig()
2542		{
2543			var proxy = Mock.Create<Base>(fluentConfig =>
2544				fluentConfig.SetBehavior(Behavior.CallOriginal)
2545			);
2546
2547			Assert.Equal(default(int), proxy.i);
2548			Assert.Null(proxy as IDisposable);
2549		}
2550#endif
2551
2552		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2553		public void ShouldThrowWhenMockConstructorAndCallConstructorWithFluentGenericConfig()
2554		{
2555			Assert.Throws<MockException>(() =>
2556				Mock.Create<Base>(fluentConfig =>
2557					fluentConfig.MockConstructor().CallConstructor(new object[] { 5 }))
2558			);
2559		}
2560
2561		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2562		public void ShouldThrowWhenCallConstructorAndMockConstructorWithFluentGenericConfig()
2563		{
2564			Assert.Throws<MockException>(() =>
2565				Mock.Create<Base>(fluentConfig =>
2566					fluentConfig.CallConstructor(new object[] { 5 }).MockConstructor())
2567			);
2568		}
2569
2570		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2571		public void ShouldSpecifyConstructorArgumentsWithFluentGenericConfig()
2572		{
2573			var proxy = Mock.Create<Base>(fluentConfig =>
2574				fluentConfig.CallConstructor(new object[] { 5 })
2575			);
2576
2577			Assert.Equal(5, proxy.i);
2578			Assert.Null(proxy as IDisposable);
2579		}
2580
2581		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2582		public void ShouldMockConstructorWithFluentGenericConfig()
2583		{
2584			var proxy = Mock.Create<Base>(fluentConfig =>
2585				fluentConfig.MockConstructor()
2586			);
2587
2588			Assert.Equal(default(int), proxy.i);
2589			Assert.Null(proxy as IDisposable);
2590		}
2591
2592		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2593		public void ShouldImplementInterfaceWithFluentGenericConfig()
2594		{
2595			var proxy = Mock.Create<Base>(fluentConfig =>
2596				fluentConfig.Implements<IDisposable>()
2597			);
2598
2599			Assert.Equal(default(int), proxy.i);
2600			Assert.NotNull(proxy as IDisposable);
2601		}
2602
2603		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2604		public void ShouldUseAutoselectedConstructorMockingBehaviorWithFluentConfig()
2605		{
2606			var proxy = (Base)Mock.Create(typeof(Base), fluentConfig => { });
2607
2608			Assert.Equal(default(int), proxy.i);
2609		}
2610
2611		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2612		public void ShouldMockWhenMissingPameterlessConstructorAndRecursiveLooseWithFluentConfig()
2613		{
2614			var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>
2615				fluentConfig.SetBehavior(Behavior.RecursiveLoose)
2616			);
2617
2618			Assert.Equal(default(int), proxy.i);
2619		}
2620
2621		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2622		public void ShouldMockWhenMissingPameterlessConstructorAndLooseWithFluentConfig()
2623		{
2624			var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>
2625				fluentConfig.SetBehavior(Behavior.Loose)
2626			);
2627
2628			Assert.Equal(default(int), proxy.i);
2629		}
2630
2631		// Implementation differs for .NETFramework and .NETCore, see DynamicProxyMockFactory.Create method
2632#if !NETCORE
2633		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2634		public void ShouldThrowWhenMissingPameterlessConstructorAndCallOriginalWithFluentConfig()
2635		{
2636			Assert.Throws<MockException>(() =>
2637				Mock.Create(typeof(Base), fluentConfig =>
2638					fluentConfig.SetBehavior(Behavior.CallOriginal))
2639				);
2640		}
2641#else
2642		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2643		public void ShouldMockWhenMissingPameterlessConstructorAndCallOriginalWithFluentConfig()
2644		{
2645			var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>
2646				fluentConfig.SetBehavior(Behavior.CallOriginal)
2647			);
2648
2649			Assert.Equal(default(int), proxy.i);
2650		}
2651#endif
2652
2653		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2654		public void ShouldThrowWhenMockConstructorAndCallConstructorWithFluentConfig()
2655		{
2656			Assert.Throws<MockException>(() =>
2657				Mock.Create(typeof(Base), fluentConfig =>
2658					fluentConfig.MockConstructor().CallConstructor(new object[] { 5 }))
2659			);
2660		}
2661
2662		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2663		public void ShouldThrowWhenCallConstructorAndMockConstructorWithFluentConfig()
2664		{
2665			Assert.Throws<MockException>(() =>
2666				Mock.Create(typeof(Base), fluentConfig =>
2667					fluentConfig.CallConstructor(new object[] { 5 }).MockConstructor())
2668			);
2669		}
2670
2671		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2672		public void ShouldSpecifyConstructorArgumentsWithFluentConfig()
2673		{
2674			var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>
2675				fluentConfig.CallConstructor(new object[] { 5 })
2676			);
2677
2678			Assert.Equal(5, proxy.i);
2679		}
2680
2681		[TestMethod, TestCategory("Lite"), TestCategory("Mock"), TestCategory("FluentConfig")]
2682		public void ShouldMockConstructorWithFluentConfig()
2683		{
2684			var proxy = (Base)Mock.Create(typeof(Base), fluentConfig =>
2685				fluentConfig.MockConstructor()
2686			);
2687
2688			Assert.Equal(default(int), proxy.i);
2689		}
2690
2691		#endregion
2692	}
2693
2694}
2695
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 methods in ClassWithNonDefaultConstructor

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)