View Code
1 /* 2 * stub和mock的微妙区别: 3 * stub对象不会使测试失败 4 * mock对象只是替换外部依赖,使测试中的对象可以运行并以测试。 5 * mock对象可以使测试失败,同时可以验证两个对象之间的交互是否按照期望的方式进行。 6 */ 7 using Mocking.Model; 8 using Mocking.ModelTest.Stubs; 9 using Mocking.ModelTest.Mocks; 10 11 using NUnit.Framework; 12 using NUnit.Mocks; 13 14 using Rhino.Mocks; 15 16 17 18 namespace Mocking.ModelTest 19 { 20 #region 手动写Stub(存根)、Mock(模拟) 21 22 [TestFixture] 23 public class HolidayRequestServiceHandRolledTest 24 { 25 26 [Test()] 27 public void The_HolidayRequestService_Will_Approve_A_HolidayRequest_ThatIs_Granted_By_The_Validator() 28 { 29 HolidayRequest holidayRequest = new HolidayRequest(); 30 AlwaysValidHolidayRequestValidator validator = new AlwaysValidHolidayRequestValidator(); 31 StubHolidayRequestRepository stubHolidayRequestRepository = new StubHolidayRequestRepository(); 32 33 //需要测试的类 34 HolidayRequestService holidayRequestService = new HolidayRequestService(validator, stubHolidayRequestRepository); 35 holidayRequest.Approved = false; 36 holidayRequestService.Submit(holidayRequest); 37 38 Assert.AreEqual(true, holidayRequest.Approved); 39 } 40 41 [Test()] 42 public void AHolidayRequestThatIsApprovedByTheRequestServiceIsSavedByTheRepsitory() 43 { 44 HolidayRequest holidayRequest = new HolidayRequest(); 45 AlwaysValidHolidayRequestValidator validator = new AlwaysValidHolidayRequestValidator(); 46 MockHolidayRequestRepository mockHolidayRequestRepository = new MockHolidayRequestRepository(); 47 48 //需要测试的类 49 HolidayRequestService holidayRequestService = new HolidayRequestService(validator, mockHolidayRequestRepository); 50 holidayRequestService.Submit(holidayRequest); 51 52 Assert.AreEqual(holidayRequest, mockHolidayRequestRepository.HolidayRequestSaved); 53 } 54 55 56 } 57 58 #endregion 59 60 #region 使用nunit.mock 61 [TestFixture] 62 public class HolidayRequestServiceHandRolledUnitMockTest 63 { 64 [Test()] 65 public void The_HolidayRequestService_Will_Approve_A_HolidayRequest_ThatIs_Granted_By_The_Validator() 66 { 67 HolidayRequest holidayRequest = new HolidayRequest(); 68 69 DynamicMock holidayRequestValidatorMock = new DynamicMock(typeof(IHolidayRequestValidator)); 70 DynamicMock holidayRequestRepositoryMock = new DynamicMock(typeof(IHolidayRequestRepository)); 71 72 holidayRequestValidatorMock.ExpectAndReturn("CanGrant", true, holidayRequest); 73 74 IHolidayRequestValidator holidayRequestValidator = (IHolidayRequestValidator)holidayRequestValidatorMock.MockInstance; 75 IHolidayRequestRepository holidayRequestRepository = (IHolidayRequestRepository)holidayRequestRepositoryMock.MockInstance; 76 77 //需要测试的类 78 HolidayRequestService holidayRequestService = new HolidayRequestService(holidayRequestValidator, holidayRequestRepository); 79 holidayRequest.Approved = false; 80 holidayRequestService.Submit(holidayRequest); 81 82 Assert.AreEqual(true, holidayRequest.Approved); 83 } 84 85 [Test()] 86 public void AHolidayRequestThatIsApprovedByTheRequestServiceIsSavedByTheRepsitory() 87 { 88 //HolidayRequest holidayRequest = new HolidayRequest(); 89 90 //DynamicMock holidayRequestValidatorMock = new DynamicMock(typeof(IHolidayRequestValidator)); 91 //DynamicMock holidayRequestRepositoryMock = new DynamicMock(typeof(IHolidayRequestRepository)); 92 //IHolidayRequestValidator holidayRequestValidator = (IHolidayRequestValidator)holidayRequestValidatorMock.MockInstance; 93 //IHolidayRequestRepository holidayRequestRepository = (IHolidayRequestRepository)holidayRequestRepositoryMock.MockInstance; 94 95 ////需要测试的类 96 //HolidayRequestService holidayRequestService = new HolidayRequestService(holidayRequestValidator, holidayRequestRepository); 97 //holidayRequestService.Submit(holidayRequest); 98 ////Assert.AreEqual(holidayRequest, holidayRequestRepository.HolidayRequestSaved); 99 } 100 } 101 102 103 #endregion 104 105 #region 使用Rhino.Mocks中的stub、mock 106 /// <summary> 107 /// 使用Rhino.Mocks 108 /// </summary> 109 [TestFixture] 110 public class HolidayRequestServiceHandRolledRhinoMocksTest 111 { 112 [Test()] 113 public void The_HolidayRequestService_Will_Approve_A_HolidayRequest_ThatIs_Granted_By_The_Validator() 114 { 115 HolidayRequest holidayRequest = new HolidayRequest(); 116 117 Rhino.Mocks.MockRepository mocks = new Rhino.Mocks.MockRepository(); 118 var validator = mocks.Stub<IHolidayRequestValidator>(); //使用反射创建该类实例 119 var stubHolidayRequestRepository = mocks.Stub<IHolidayRequestRepository>(); 120 121 using (mocks.Record()) //设置存根类的方法被调用时该存根类的响应方式 122 { 123 //为stub(存根)设置返回值 124 SetupResult.For(validator.CanGrant(holidayRequest)).Return(true); 125 } 126 127 using (mocks.Playback()) 128 { 129 //需要测试的类 130 HolidayRequestService holidayRequestService = new HolidayRequestService(validator, stubHolidayRequestRepository); 131 holidayRequest.Approved = false; 132 holidayRequestService.Submit(holidayRequest); 133 Assert.AreEqual(true, holidayRequest.Approved); 134 } 135 136 } 137 138 [Test()] 139 public void AHolidayRequestThatIsApprovedByTheRequestServiceIsSavedByTheRepsitory() 140 { 141 HolidayRequest holidayRequest = new HolidayRequest(); 142 143 Rhino.Mocks.MockRepository mocks = new Rhino.Mocks.MockRepository(); 144 var validator = mocks.Stub<IHolidayRequestValidator>(); 145 //var mockHolidayRequestRepository = mocks.CreateMock<IHolidayRequestRepository>(); 146 var mockHolidayRequestRepository = mocks.StrictMock<IHolidayRequestRepository>(); 147 148 using (mocks.Record()) 149 { 150 //设置返回值 151 SetupResult.For(validator.CanGrant(holidayRequest)).Return(true); 152 153 mockHolidayRequestRepository.Save(holidayRequest); 154 LastCall.Repeat.Once(); 155 } 156 157 using (mocks.Playback()) 158 { 159 //需要测试的类 160 HolidayRequestService holidayRequestService = new HolidayRequestService(validator, mockHolidayRequestRepository); 161 holidayRequest.Approved = false; 162 holidayRequestService.Submit(holidayRequest); 163 164 //注意:该测试没有调用断言语句,那在哪里进行验证调用???????????? 165 166 } 167 } 168 169 } 170 #endregion 171 172 #region 框架Moq 173 174 [TestFixture] 175 public class HolidayRequestServiceHandRolledUsingMoq 176 { 177 [Test()] 178 public void The_HolidayRequestService_Will_Approve_A_HolidayRequest_ThatIs_Granted_By_The_Validator() 179 { 180 HolidayRequest holidayRequest = new HolidayRequest(); 181 var holidayRequestValidator = new Moq.Mock<IHolidayRequestValidator>(); 182 var holidayRequestRepository = new Moq.Mock<IHolidayRequestRepository>(); 183 184 holidayRequestValidator.Setup(validator => validator.CanGrant(holidayRequest)).Returns(true); 185 186 HolidayRequestService holidayRequestService = new HolidayRequestService(holidayRequestValidator.Object, holidayRequestRepository.Object); 187 188 holidayRequest.Approved = false; 189 holidayRequestService.Submit(holidayRequest); 190 191 Assert.AreEqual(true, holidayRequest.Approved); 192 } 193 194 [Test()] 195 public void AHolidayRequestThatIsApprovedByTheRequestServiceIsSavedByTheRepsitory() 196 { 197 HolidayRequest holidayRequest = new HolidayRequest(); 198 var holidayRequestValidator = new Moq.Mock<IHolidayRequestValidator>(); 199 var holidayRequestRepository = new Moq.Mock<IHolidayRequestRepository>(); 200 201 //需要测试的类 202 HolidayRequestService holidayRequestService = new HolidayRequestService(holidayRequestValidator.Object,holidayRequestRepository.Object); 203 holidayRequestService.Submit(holidayRequest); 204 205 //Assert.AreEqual(holidayRequest, holidayRequestRepository.Object.Save()); 206 } 207 } 208 #endregion 209 }