During a recent XP programming course we made use of the Moq’ing framework. I’d not used this before so tried to come up with some example moq basic tests. There are several tutorials on the internet which offer up the basics so do hunt around. Hopefully the following provides some simple examples for people to get started with.
The following code relies on the following assemblies:
- nunit.framework – available from http://www.nunit.org/
- moq – available from http://code.google.com/p/moq/
The real crux of TDD is that you program your functionality to interfaces – here we have a simple interface and dto:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public interface IMyInterface { int IntMethod(int value); int HarderIntMethod(int value, int anotherValue); MyObject MyObjectMethod(int value); } public class MyObject { public virtual string MyVirtualProperty { get; set; } public string MyProperty { get; set; } } |
Note the importance of the virtual property in MyObject will be highlighted in the tests. In the following examples not all the tests pass – this is intentional! Failing tests are included to demonstrate specific features of Moq. Apologies for the long dump of test code – the idea is really to copy it into a test class and let NUnit do the work.
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
[TestFixture] public class MyTests { [Test] [TestCase(1, TestName = "1")] public void A_TestMockBehaviourDefault(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(); int result = myInterface.Object.IntMethod(value); //we havent setup mock for value = 1 Assert.AreEqual(value, result); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] public void B_TestMockBehaviourStrict(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); int result = myInterface.Object.IntMethod(value); //now in strict mode //again, we havent setup mock for value = 1 Assert.AreEqual(value, result); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void C_TestMockBehaviourStrictWithSpecificValue(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); myInterface.Setup(a => a.IntMethod(1)).Returns(1); int result = myInterface.Object.IntMethod(value); //we have setup the mock for value = 1 but not value = 2 Assert.AreEqual(value, result); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void D_TestMockBehaviourStrictWithTypedValue(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); myInterface.Setup(a => a.IntMethod(It.IsAny<int>())).Returns((int a) => a); int result = myInterface.Object.IntMethod(value); //any int will pass Assert.AreEqual(value, result); myInterface.Verify(); } private int _count = 0; [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void E_TestMockBehaviourStrictWithTypedValueAndCallback(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); myInterface.Setup(a => a.IntMethod(It.IsAny<int>())).Returns((int a) => a).Callback(() => _count++); int result = myInterface.Object.IntMethod(value); //we can log callbacks before or after the Returns call Console.WriteLine(String.Format("Called E_TestMockBehaviourStrictWithTypedValueAndCallback {0} times", _count)); Assert.AreEqual(value, result); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void E1_TestMockBehaviourStrictWithTypedValueAndCallbackBefore(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //callbacks can happen before and after the Returns call myInterface.Setup(a => a.IntMethod(It.IsAny<int>())).Callback(() => Console.WriteLine("before")).Returns((int a) => a).Callback(() => Console.WriteLine("after")); int result = myInterface.Object.IntMethod(value); Assert.AreEqual(value, result); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] [ExpectedException(typeof(Exception))] public void F_TestMockBehaviourStrictWithException(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //this can throw specific exceptions if we want //catching at a test level is a bit loose since other code could exception - see F1 for alternative myInterface.Setup(a => a.IntMethod(It.IsAny<int>())).Throws<Exception>(); int result = myInterface.Object.IntMethod(value); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void F1_TestMockBehaviourStrictWithException(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //this can throw specific exceptions if we want myInterface.Setup(a => a.IntMethod(It.IsAny<int>())).Throws<Exception>(); //here we can catch specific exceptions Assert.Throws<Exception>(() => myInterface.Object.IntMethod(value)); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void G_TestMockBehaviourStrictVerify(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); myInterface.Setup(a => a.IntMethod(It.IsAny<int>())).Returns((int a) => a).Verifiable(); myInterface.Setup(a => a.HarderIntMethod(It.IsAny<int>(), (It.IsAny<int>()))).Returns((int a, int b) => a).Verifiable(); int result = myInterface.Object.IntMethod(value); //we havent called HarderIntMethod myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void H_TestMockBehaviourStrictVerifySuccess(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); myInterface.Setup(a => a.IntMethod(It.IsAny<int>())).Returns((int a) => a).Verifiable(); myInterface.Setup(a => a.HarderIntMethod(It.IsAny<int>(), It.IsAny<int>())).Returns((int a, int b) => a).Verifiable(); int result = myInterface.Object.IntMethod(value); int anotherResult = myInterface.Object.HarderIntMethod(value, value); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void I_TestMockBehaviourStrictWithTypedValueAndLogicOnInput(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //setup can perform logic based on input to determine return value(s) myInterface.Setup(a => a.IntMethod(It.Is<int>(b => b % 2 == 0))).Returns((int a) => a); myInterface.Setup(a => a.IntMethod(It.Is<int>(b => b % 2 != 0))).Returns((int a) => -a); int result = myInterface.Object.IntMethod(value); Assert.AreEqual(value, result); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] [TestCase(4, TestName = "4")] public void J_TestMockBehaviourStrictWithTypedValueAndRangeLogicOnInput(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //rather than specific logic, instead we can make use of Ranges myInterface.Setup(a => a.IntMethod(It.IsInRange<int>(0,3, Range.Inclusive))).Returns((int a) => a); int result = myInterface.Object.IntMethod(value); Assert.AreEqual(value, result); //we haven't setup a return value for 4 myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void K_TestMockBehaviourStrictWithProperties(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //we can manipulate objects being returned myInterface.Setup(a => a.MyObjectMethod(value)).Returns((int a) => new MyObject() { MyProperty = a.ToString() }); MyObject result = myInterface.Object.MyObjectMethod(value); Assert.AreEqual(value.ToString(), result.MyProperty); myInterface.Verify(); } [Test] public void L_TestMockBehaviourStrictWithPropertiesVerifyGet() { Mock<MyObject> myObject = new Mock<MyObject>(MockBehavior.Strict); //this is where you need virtual properties myObject.Setup(a => a.MyProperty).Returns("blah"); myObject.Verify(); myObject.VerifyGet(a => a.MyProperty); } [Test] public void L1_TestMockBehaviourStrictWithPropertiesVerifyGet() { Mock<MyObject> myObject = new Mock<MyObject>(MockBehavior.Strict); myObject.Setup(a => a.MyVirtualProperty).Returns("blah"); myObject.Verify(); //we have never accessed MyVirtualProperty hence the VerifyGet fails myObject.VerifyGet(a => a.MyVirtualProperty); } [Test] public void L2_TestMockBehaviourStrictWithPropertiesVerifyGet() { Mock<MyObject> myObject = new Mock<MyObject>(MockBehavior.Strict); myObject.Setup(a => a.MyVirtualProperty).Returns("blah"); Assert.IsNotEmpty(myObject.Object.MyVirtualProperty); myObject.Verify(); //we have accessed both properties myObject.VerifyGet(a => a.MyVirtualProperty); } [Test] public void L3_TestMockBehaviourStrictWithPropertiesSetupProperty() { Mock<MyObject> myObject = new Mock<MyObject>(MockBehavior.Strict); //rather than setup and then adding Returns method, SetupProperty achieves the same thing myObject.SetupProperty(a => a.MyVirtualProperty, "blah"); Assert.IsNotEmpty(myObject.Object.MyVirtualProperty); myObject.Verify(); myObject.VerifyGet(a => a.MyVirtualProperty); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void M_TestMockBehaviourStrictWithTypedValueAndDetailedCallback(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //Callback and Returns can both deal with multiple parameters via Generics or parameters. // M1 shows the opposite of each myInterface.Setup(a => a.HarderIntMethod(It.IsAny<int>(), It.IsAny<int>())) .Returns((int a, int b) => a) .Callback<int, int>((a, b) => Console.WriteLine("A is " + a + " B is " + b)); int result = myInterface.Object.HarderIntMethod(value, value + 1); Assert.AreEqual(value, result); myInterface.Verify(); } [Test] [TestCase(1, TestName = "1")] [TestCase(2, TestName = "2")] public void M1_TestMockBehaviourStrictWithTypedValueAndDetailedCallbackAlternativeSyntax(int value) { Mock<IMyInterface> myInterface = new Mock<IMyInterface>(MockBehavior.Strict); //demonstrates the opposite of M in terms of the syntax required into Callback and Returns myInterface.Setup(a => a.HarderIntMethod(It.IsAny<int>(), It.IsAny<int>())) .Returns<int , int>((a, b) => a) .Callback((int a, int b) => Console.WriteLine("A is " + a + " B is " + b)); int result = myInterface.Object.HarderIntMethod(value, value + 1); Assert.AreEqual(value, result); myInterface.Verify(); } } |
Here is the expected results when evaluated in NUnit: