File: DynamicMockTests.cs

package info (click to toggle)
nunit2.2 2.2.0-3.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,164 kB
  • ctags: 4,237
  • sloc: cs: 25,295; sh: 80; xml: 76; cpp: 70; makefile: 56; ansic: 7
file content (162 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using NUnit.Framework;
using NUnit.Mocks;

namespace NUnit.Tests.Mocks
{
	/// <summary>
	/// Summary description for DynamicMockTests.
	/// </summary>
	[TestFixture]
	public class DynamicMockTests
	{
		private DynamicMock mock;
		private IStuff instance;

		[SetUp]
		public void CreateMock()
		{
			mock = new DynamicMock( typeof( IStuff ) );
			instance = (IStuff)mock.MockInstance;
		}

		[Test]
		public void MockHasDefaultName()
		{
			Assert.AreEqual( "MockIStuff", mock.Name );
		}

		[Test]
		public void MockHasNonDefaultName()
		{
			DynamicMock mock2 = new DynamicMock( "MyMock", typeof( IStuff ) );
			Assert.AreEqual( "MyMock", mock2.Name );
		}

		[Test]
		public void CallMethod()
		{
			instance.DoSomething();
			mock.Verify();
		}

		[Test]
		public void CallMethodWithArgs()
		{
			instance.DoSomething( "hello" );
			mock.Verify();
		}

		[Test]
		public void ExpectedMethod()
		{
			mock.Expect( "DoSomething" );
			instance.DoSomething();
			mock.Verify();
		}

		[Test, ExpectedException( typeof(AssertionException) )]
		public void ExpectedMethodNotCalled()
		{
			mock.Expect( "DoSomething" );
			mock.Verify();
		}

		[Test]
		public void MethodWithReturnValue()
		{
			mock.SetReturnValue( "GetInt", 5 );
			Assert.AreEqual( 5, instance.GetInt() );
			mock.Verify();
		}

		[Test]
		public void DefaultReturnValues()
		{
			Assert.AreEqual( 0, instance.GetInt(), "int" );
			Assert.AreEqual( 0, instance.GetSingle(), "float" );
			Assert.AreEqual( 0, instance.GetDouble(), "double" );
			Assert.AreEqual( 0, instance.GetDecimal(), "decimal" );
			Assert.AreEqual( '?', instance.GetChar(), "char" );
			mock.Verify();
		}

		[Test, ExpectedException( typeof(InvalidCastException) )]
		public void WrongReturnType()
		{
			mock.SetReturnValue( "GetInt", "hello" );
			instance.GetInt();
			mock.Verify();
		}

		[Test]
		public void OverrideMethodOnDynamicMock()
		{
			DynamicMock derivedMock = new DerivedMock();
			IStuff derivedInstance = (IStuff)derivedMock.MockInstance;
			Assert.AreEqual( 17, derivedInstance.Add( 5, 12 ) );
			derivedMock.Verify();
		}

		[Test, ExpectedException( typeof(ArgumentException) )]
		public void CreateMockForNonMBRClassFails()
		{
			DynamicMock classMock = new DynamicMock( typeof( NonMBRClass ) );
			NonMBRClass classInstance = (NonMBRClass)classMock.MockInstance;
		}

		[Test]
		public void CreateMockForMBRClass()
		{
			DynamicMock classMock = new DynamicMock( typeof( MBRClass ) );
			MBRClass classInstance = (MBRClass)classMock.MockInstance;
			classMock.Expect( "SomeMethod" );
			classMock.ExpectAndReturn( "AnotherMethod", "Hello World", 5, "hello" );
			classInstance.SomeMethod();
			Assert.AreEqual( "Hello World", classInstance.AnotherMethod( 5, "hello" ) );
			classMock.Verify();
		}

		#region Test Interfaces and Classes

		interface IStuff
		{
			void DoSomething();
			void DoSomething( string greeting );
			int GetInt();
			float GetSingle();
			double GetDouble();
			decimal GetDecimal();
			char GetChar();
			int Add( int a, int b );
		}

		class DerivedMock : DynamicMock
		{
			public DerivedMock() : base( "Derived", typeof( IStuff ) ) { }

			public override object Call( string methodName, params object[] args )
			{
				switch( methodName )
				{
					case "Add":
						return (int)args[0] + (int)args[1];
					default:
						return base.Call( methodName, args );
				}
			}
		}

		class NonMBRClass
		{
		}

		class MBRClass : MarshalByRefObject
		{
			public void SomeMethod(){ }
			public string AnotherMethod( int a, string b ) { return b + a.ToString(); }
		}

		#endregion
	}
}