How to use EmptyTestClassAttribute class of Telerik.JustMock.XUnit.Test.Attributes package

Best JustMockLite code snippet using Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute

Run JustMockLite automation tests on LambdaTest cloud grid

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

AsyncFixture.cs

Source: AsyncFixture.cs Github

copy
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
5#region JustMock Test Attributes
6#if NUNIT
7using NUnit.Framework;
8using TestCategory = NUnit.Framework.CategoryAttribute;
9using TestClass = NUnit.Framework.TestFixtureAttribute;
10using TestMethod = NUnit.Framework.TestAttribute;
11using TestInitialize = NUnit.Framework.SetUpAttribute;
12using TestCleanup = NUnit.Framework.TearDownAttribute;
13using AssertionException = NUnit.Framework.AssertionException;
14#elif XUNIT
15using Xunit;
16using Telerik.JustMock.XUnit.Test.Attributes;
17using TestCategory = Telerik.JustMock.XUnit.Test.Attributes.XUnitCategoryAttribute;
18using TestClass = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute;
19using TestMethod = Xunit.FactAttribute;
20using TestInitialize = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestInitializeAttribute;
21using TestCleanup = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestCleanupAttribute;
22using AssertionException = Telerik.JustMock.XUnit.AssertFailedException;
23#elif VSTEST_PORTABLE
24using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
25using AssertionException = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException;
26#else
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28using AssertionException = Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException;
29#endif
30#endregion
31
32namespace Telerik.JustMock.Tests
33{
34	[TestClass]
35	public class AsyncFixture
36	{
37		[TestMethod, TestCategory("Async"), TestCategory("Lite")]
38		public async Task ShouldRetainMockingContextInContinuation()
39		{
40			var model = Mock.Create<IAsyncModel>();
41			Mock.Arrange(() => model.GetData()).Returns(5);
42			var controller = new TestAsyncController();
43			await controller.DoStuff(model);
44			//no exception
45		}
46
47		[TestMethod, TestCategory("Async"), TestCategory("Lite")]
48		public void ShouldCreateRecursiveMockInConstructorOnAnotherThread()
49		{
50			Mock.Arrange(() => Arg.IsAny<ThreadCtor>().Assign()).CallOriginal();
51			var mock = Mock.Create<ThreadCtor>(Constructor.NotMocked, Behavior.RecursiveLoose);
52			Assert.NotNull(mock.TheItem);
53		}
54
55		public abstract class ThreadCtor
56		{
57			protected abstract IDisposable Item { get; }
58
59			public ThreadCtor()
60			{
61				var thread = new Thread(this.Assign);
62				thread.Start();
63				if (!thread.Join(TimeSpan.FromMilliseconds(1500)))
64					throw new TimeoutException();
65			}
66
67			public virtual void Assign()
68			{
69				TheItem = Item;
70			}
71
72			public IDisposable TheItem;
73		}
74	}
75
76	public interface IAsyncModel
77	{
78		int GetData();
79	}
80
81	public class TestAsyncController
82	{
83		public async Task DoStuff(IAsyncModel model)
84		{
85			await Task.Factory.StartNew(() => Thread.Sleep(5));
86			// continuation begins here
87			var result = model.GetData();
88			if (result != 5)
89			{
90				throw new Exception();
91			}
92		}
93	}
94}
95
Full Screen

SerializableFixture.cs

Source: SerializableFixture.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 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*/
17using System;
18using System.Collections.Generic;
19using System.Text;
20using Telerik.JustMock.DemoLib;
21
22#region JustMock Test Attributes
23#if NUNIT
24using NUnit.Framework;
25using TestCategory = NUnit.Framework.CategoryAttribute;
26using TestClass = NUnit.Framework.TestFixtureAttribute;
27using TestMethod = NUnit.Framework.TestAttribute;
28using TestInitialize = NUnit.Framework.SetUpAttribute;
29using TestCleanup = NUnit.Framework.TearDownAttribute;
30using AssertionException = NUnit.Framework.AssertionException;
31#elif XUNIT
32using Xunit;
33using Telerik.JustMock.XUnit.Test.Attributes;
34using TestCategory = Telerik.JustMock.XUnit.Test.Attributes.XUnitCategoryAttribute;
35using TestClass = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute;
36using TestMethod = Xunit.FactAttribute;
37using TestInitialize = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestInitializeAttribute;
38using TestCleanup = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestCleanupAttribute;
39using AssertionException = Telerik.JustMock.XUnit.AssertFailedException;
40#elif VSTEST_PORTABLE
41using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
42using AssertionException = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException;
43#else
44using Microsoft.VisualStudio.TestTools.UnitTesting;
45using AssertionException = Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException;
46#endif
47#endregion
48
49namespace Telerik.JustMock.Tests
50{
51    [TestClass]
52    public class SerializableFixture
53    {
54        [TestMethod, TestCategory("Lite"), TestCategory("Serializable")]
55        public void ShouldMockTypesMarkedWithSerializableAttribute()
56        {
57            int expected = 10;
58            var foo = Mock.Create<FooSerializable>();
59
60            Mock.Arrange(() => foo.Value).Returns(expected);
61
62            var actual = foo.Value;
63            Assert.Equal(expected, actual);
64        }
65
66        [TestMethod, TestCategory("Lite"), TestCategory("Serializable")]
67        public void ShouldMockTypesThatInheritISerializable()
68        {
69            int expected = 10;
70            var foo = Mock.Create<FooInheritISerializable>();
71
72            Mock.Arrange(() => foo.Value).Returns(expected);
73
74            var actual = foo.Value;
75            Assert.Equal(expected, actual);
76        }
77
78        [TestMethod, TestCategory("Lite"), TestCategory("Serializable")]
79        public void ShouldMockTypeThatImplementSerializationAttributeAndInterface()
80        {
81            int expected = 10;
82            var foo = Mock.Create<FooImplementSerializationAttributeAndInterface>();
83
84            Mock.Arrange(() => foo.Value).Returns(expected);
85
86            var actual = foo.Value;
87            Assert.Equal(expected, actual);
88        }
89    }
90}
91
Full Screen

InteropFixture.cs

Source: InteropFixture.cs Github

copy
1using System;
2
3#region JustMock Test Attributes
4#if NUNIT
5using NUnit.Framework;
6using TestCategory = NUnit.Framework.CategoryAttribute;
7using TestClass = NUnit.Framework.TestFixtureAttribute;
8using TestMethod = NUnit.Framework.TestAttribute;
9using TestInitialize = NUnit.Framework.SetUpAttribute;
10using TestCleanup = NUnit.Framework.TearDownAttribute;
11using AssertionException = NUnit.Framework.AssertionException;
12#elif XUNIT
13using Xunit;
14using Telerik.JustMock.XUnit.Test.Attributes;
15using TestCategory = Telerik.JustMock.XUnit.Test.Attributes.XUnitCategoryAttribute;
16using TestClass = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestClassAttribute;
17using TestMethod = Xunit.FactAttribute;
18using TestInitialize = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestInitializeAttribute;
19using TestCleanup = Telerik.JustMock.XUnit.Test.Attributes.EmptyTestCleanupAttribute;
20using AssertionException = Telerik.JustMock.XUnit.AssertFailedException;
21#elif VSTEST_PORTABLE
22using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
23using AssertionException = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AssertFailedException;
24#else
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26using AssertionException = Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException;
27#endif
28#endregion
29
30namespace Telerik.JustMock.Tests
31{
32	[TestClass]
33	public unsafe class InteropFixture
34	{
35		public abstract class UnsafeClass1
36		{
37			public abstract void Do(void* ptr);
38		}
39
40		public abstract class UnsafeClass2
41		{
42			public abstract void* Alloc();
43
44			public abstract int* Realloc(int* x);
45		}
46
47		[TestMethod, TestCategory("Interop")]
48		public void ShouldArrangeMethodWithPointerArg()
49		{
50			var mock = Mock.Create<UnsafeClass1>();
51			int called = 0;
52
53			Mock.ArrangeSet<UnsafeClass1>(() => mock.Do((void*)0)).IgnoreArguments().DoInstead(() => called++);
54			mock.Do((void*)0);
55			mock.Do((void*)123);
56			Assert.Equal(2, called);
57		}
58
59		[TestMethod, TestCategory("Interop")]
60		public void ShouldArrangeMethodWithPointerReturnType()
61		{
62			var mock = Mock.Create<UnsafeClass2>();
63			Mock.Arrange(mock, x => (IntPtr)x.Alloc()).Returns(new IntPtr(1234));
64			var result = mock.Alloc();
65			Assert.Equal(1234, new IntPtr(result).ToInt32());
66		}
67
68		[TestMethod, TestCategory("Interop")]
69		public void ShouldPassPointersToAndFromReturns()
70		{
71			var mock = Mock.Create<UnsafeClass2>();
72			Mock.Arrange(mock, x => (IntPtr)x.Realloc((int*)0)).IgnoreArguments().Returns((IntPtr a) => a + 1000);
73			var result = mock.Realloc((int*)123);
74			Assert.Equal(1123, (int)result);
75		}
76
77		public abstract class UnsafeClassRef
78		{
79			public abstract void Do(ref void* arg);
80		}
81
82		public delegate void DoDelegate(ref IntPtr arg);
83
84		[TestMethod, TestCategory("Interop")]
85		public void ShouldPassPointersByRef()
86		{
87			var mock = Mock.Create<UnsafeClassRef>();
88
89			void* arg = null;
90			Mock.NonPublic.Arrange(mock, "Do").DoInstead(new DoDelegate((ref IntPtr x) => x += 500));
91
92			arg = (void*)1000;
93			mock.Do(ref arg);
94			Assert.Equal(1500, (int)arg);
95		}
96
97		public class UnsafeClass3
98		{
99			public virtual void* Do(void* x)
100			{
101				return (void*)((IntPtr)x + 1000);
102			}
103		}
104
105		[TestMethod, TestCategory("Interop")]
106		public void ShouldCallOriginalImplementationWithPointers()
107		{
108			var mock = Mock.Create<UnsafeClass3>();
109			Mock.Arrange(mock, x => x.Do(null)).IgnoreArguments().When((IntPtr x) => x.ToInt32() > 1000).CallOriginal();
110
111			Assert.Equal(0, (int)mock.Do((void*)100));
112			Assert.Equal(2500, (int)mock.Do((void*)1500));
113		}
114
115	}
116}
117
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 EmptyTestClassAttribute

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)