public void ScenarioPlayTest()
        {
            using (RecorderManager.NewRecordingSession("test"))
            {
                // Make a callee, not really used but needed to record a constructor call:
                MockingProxy callee = new MockingProxy(typeof(Sample.Account), null, "m1");

                // Push some calls in the recorder:
                MockableCall lastcall;
                CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetConstructor(new Type[] { typeof(Sample.CurrencyUnit) }), null));
                lastcall.SetConstructionResult("acc");
                CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetMethod("Deposit"), null));
                lastcall.SetCallResult();
                CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetMethod("Withdraw"), null));
                lastcall.SetCallResult();
                CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetProperty("Balance").GetGetMethod(), null));
                lastcall.SetCallResult(10m);
            }

            using (RecorderManager.NewPlayBackSession("test", true))
            {
                // Register types to mock:
                MockService.AddTypeToMock(typeof(Sample.Account));

                // Play scenario:
                Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
                acc.Deposit(100m);
                acc.Withdraw(25m);
                Decimal balance = acc.Balance;

                // Checks:
                Assert.AreEqual(10m, balance);                 // Does not match the scenario, but the mocking result !
            }
        }
        public void ManualMockingScenarioTest()
        {
            ManualMockSession sess = new ManualMockSession();

            Sample.ICurrencyService cv = (Sample.ICurrencyService)sess.Mock(typeof(Sample.ICurrencyService), "cs")
                                         .ExpectAndReturn("ConvertAmount", 50m)
                                         .ExpectAndReturn("ConvertAmount", 80m).WithArguments(50m, ExpectedValue.OfType(typeof(Sample.CurrencyUnit)), ExpectedValue.OfType(typeof(Sample.CurrencyUnit)))
                                         .ExpectAndReturn("ConvertAmount", 30m).WithArguments(ExpectedValue.Any, ExpectedValue.OfType(typeof(Sample.CurrencyUnit)), Sample.CurrencyUnit.EUR).RepeatTimes(3)
                                         .Mock;
            Sample.CurrencyServiceFactory.NextInstance = cv;
            Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
            acc.Deposit(100m);
            Assert.AreEqual(100m, acc.Balance);
            acc.SwitchCurrency(Sample.CurrencyUnit.GBP);
            Assert.AreEqual(50m, acc.Balance);
            acc.SwitchCurrency(Sample.CurrencyUnit.USD);
            Assert.AreEqual(80m, acc.Balance);
            acc.SwitchCurrency(Sample.CurrencyUnit.EUR);
            Assert.AreEqual(30m, acc.Balance);
            acc.SwitchCurrency(Sample.CurrencyUnit.EUR);
            Assert.AreEqual(30m, acc.Balance);
            try
            {
                acc.SwitchCurrency(Sample.CurrencyUnit.NULL);
                Assert.Fail("Exception expected.");
            }
            catch (ReplayMockException) { }
            sess.ValidateSession();
        }
Esempio n. 3
0
 public void MockNotCreatedTest()
 {
     // When type not added with AddTypeToMock...
     using (RecorderManager.NewRecordingSession("test"))
     {
         Assert.AreEqual(RecorderState.Recording, RecorderManager.Action);
         Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
         Assert.IsFalse(MockService.IsMock(acc));
     }
 }
Esempio n. 4
0
 public void MockCreatedTest()
 {
     using (RecorderManager.NewRecordingSession("test"))
     {
         Assert.AreEqual(RecorderState.Recording, RecorderManager.Action);
         MockService.AddTypeToMock(typeof(Sample.Account));
         Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
         Assert.IsTrue(MockService.IsMock(acc));
     }
 }
		public void MockCreatedTest()
		{
			using (RecorderManager.NewRecordingSession("test"))
			{
				Assert.AreEqual(RecorderState.Recording, RecorderManager.Action);
				MockService.AddTypeToMock(typeof(Sample.Account));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				Assert.IsTrue(MockService.IsMock(acc));
			}
		}
		public void MockNotCreatedTest()
		{
			// When type not added with AddTypeToMock...
			using (RecorderManager.NewRecordingSession("test"))
			{
				Assert.AreEqual(RecorderState.Recording, RecorderManager.Action);
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				Assert.IsFalse(MockService.IsMock(acc));
			}
		}
 public void DeepCustomMockingTest()
 {
     using (RecorderManager.NewPlayBackSession("test", false))
     {
         MockService.AddTypeToMock(typeof(Sample.CustomizableCurrencyService));
         Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
         acc.Deposit(100m);
         acc.SwitchCurrency(Sample.CurrencyUnit.USD);
         Assert.AreEqual(100m, acc.Balance);
     }
 }
		public void DeepCustomMockingTest()
		{
			using (RecorderManager.NewPlayBackSession("test", false))
			{
				MockService.AddTypeToMock(typeof(Sample.CustomizableCurrencyService));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				acc.Deposit(100m);
				acc.SwitchCurrency(Sample.CurrencyUnit.USD);
				Assert.AreEqual(100m, acc.Balance);
			}
		}
		public void MessageCompletedTest()
		{
			using (RecorderManager.NewRecordingSession("test"))
			{
				MockService.AddTypeToMock(typeof(Sample.Account));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				acc.Deposit(100m);
				decimal v = acc.Balance;
			}
			Assert.IsTrue(CurrentRecorder[0].IsCompleted);
			Assert.IsTrue(CurrentRecorder[1].IsCompleted);
			Assert.IsTrue(CurrentRecorder[2].IsCompleted);
		}
Esempio n. 10
0
 public void MessageCompletedTest()
 {
     using (RecorderManager.NewRecordingSession("test"))
     {
         MockService.AddTypeToMock(typeof(Sample.Account));
         Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
         acc.Deposit(100m);
         decimal v = acc.Balance;
     }
     Assert.IsTrue(CurrentRecorder[0].IsCompleted);
     Assert.IsTrue(CurrentRecorder[1].IsCompleted);
     Assert.IsTrue(CurrentRecorder[2].IsCompleted);
 }
Esempio n. 11
0
		public void MessageTest()
		{
			using (RecorderManager.NewRecordingSession("test"))
			{
				MockService.AddTypeToMock(typeof(Sample.Account));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				acc.Deposit(100m);
				decimal v = acc.Balance;
			}
			Assert.IsTrue(CurrentRecorder[0].IsConstructorCall);
			Assert.IsFalse(CurrentRecorder[1].IsConstructorCall);
			Assert.AreEqual("Deposit", CurrentRecorder[1].Method.Name);
			Assert.AreEqual(1, CurrentRecorder[1].InArgs.Length);
			Assert.AreEqual(100m, CurrentRecorder[1].InArgs[0]);
			Assert.IsNull(CurrentRecorder[1].ReturnValue);
			Assert.IsFalse(CurrentRecorder[2].IsConstructorCall);
			Assert.AreEqual("get_Balance", CurrentRecorder[2].Method.Name);
			Assert.AreEqual(0, CurrentRecorder[2].InArgs.Length);
			Assert.AreEqual(100m, CurrentRecorder[2].ReturnValue);
		}
Esempio n. 12
0
 public void MessageTest()
 {
     using (RecorderManager.NewRecordingSession("test"))
     {
         MockService.AddTypeToMock(typeof(Sample.Account));
         Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
         acc.Deposit(100m);
         decimal v = acc.Balance;
     }
     Assert.IsTrue(CurrentRecorder[0].IsConstructorCall);
     Assert.IsFalse(CurrentRecorder[1].IsConstructorCall);
     Assert.AreEqual("Deposit", CurrentRecorder[1].Method.Name);
     Assert.AreEqual(1, CurrentRecorder[1].InArgs.Length);
     Assert.AreEqual(100m, CurrentRecorder[1].InArgs[0]);
     Assert.IsNull(CurrentRecorder[1].ReturnValue);
     Assert.IsFalse(CurrentRecorder[2].IsConstructorCall);
     Assert.AreEqual("get_Balance", CurrentRecorder[2].Method.Name);
     Assert.AreEqual(0, CurrentRecorder[2].InArgs.Length);
     Assert.AreEqual(100m, CurrentRecorder[2].ReturnValue);
 }
Esempio n. 13
0
        public void RecordAndPlaybackTest()
        {
            decimal balanceToBe;

            Assert.AreEqual(RecorderState.None, RecorderManager.Action);
            using (RecorderManager.NewRecordingSession("test"))
            {
                Assert.AreEqual(RecorderState.Recording, RecorderManager.Action);
                MockService.AddTypeToMock(typeof(Sample.Account));
                MockService.AddTypeToMock(typeof(Sample.CustomizableCurrencyService));
                Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
                Assert.IsTrue(MockService.IsMock(acc));
                acc.Deposit(100m);
                acc.SwitchCurrency(Sample.CurrencyUnit.USD);
                acc.Withdraw(50m);
                balanceToBe = acc.Balance;
            }
            Assert.AreEqual(RecorderState.None, RecorderManager.Action);
            Assert.AreEqual(5, CurrentRecorder.Count);
            Assert.IsTrue(CurrentRecorder[0].IsConstructorCall);
            Assert.AreEqual("Deposit", CurrentRecorder[1].Method.Name);
            Assert.AreEqual("SwitchCurrency", CurrentRecorder[2].Method.Name);
            Assert.AreEqual("Withdraw", CurrentRecorder[3].Method.Name);
            Assert.AreEqual("get_Balance", CurrentRecorder[4].Method.Name);
            Assert.AreEqual(balanceToBe, CurrentRecorder[4].ReturnValue);
            using (RecorderManager.NewPlayBackSession("test", false))
            {
                Assert.AreEqual(RecorderState.PlayBack, RecorderManager.Action);
                MockService.AddTypeToMock(typeof(Sample.Account));
                MockService.AddTypeToMock(typeof(Sample.CustomizableCurrencyService));
                Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
                Assert.IsTrue(MockService.IsMock(acc));
                acc.Deposit(100m);
                acc.SwitchCurrency(Sample.CurrencyUnit.USD);
                acc.Withdraw(50m);
                Assert.AreEqual(balanceToBe, acc.Balance);
                RecorderManager.ValidatePlayBack();
            }
            Assert.AreEqual(RecorderState.None, RecorderManager.Action);
            Assert.IsTrue(CurrentRecorder.Validated);
        }
Esempio n. 14
0
 public void PropertyMock()
 {
     using (RecorderManager.NewRecordingSession("test"))
     {
         MockService.AddTypeToMock(typeof(Sample.Account));
         Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
         Assert.IsTrue(MockService.IsMock(acc));
         acc.Deposit(100m);
         acc.Withdraw(50m);
         Assert.IsTrue(acc.IsBalancePositive);
     }
     using (RecorderManager.NewPlayBackSession("test", true))
     {
         MockService.AddTypeToMock(typeof(Sample.Account));
         Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
         Assert.IsTrue(MockService.IsMock(acc));
         acc.Deposit(100m);
         acc.Withdraw(50m);
         Assert.IsTrue(acc.IsBalancePositive);
     }
 }
		public void ManualMockingScenarioTest()
		{
			ManualMockSession sess = new ManualMockSession();
			Sample.ICurrencyService cv = (Sample.ICurrencyService)sess.Mock(typeof(Sample.ICurrencyService), "cs")
				.ExpectAndReturn("ConvertAmount", 50m)
				.ExpectAndReturn("ConvertAmount", 80m).WithArguments(50m, ExpectedValue.OfType(typeof(Sample.CurrencyUnit)), ExpectedValue.OfType(typeof(Sample.CurrencyUnit)))
				.ExpectAndReturn("ConvertAmount", 30m).WithArguments(ExpectedValue.Any, ExpectedValue.OfType(typeof(Sample.CurrencyUnit)), Sample.CurrencyUnit.EUR).RepeatTimes(3)
				.Mock;
			Sample.CurrencyServiceFactory.NextInstance = cv;
			Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
			acc.Deposit(100m);
			Assert.AreEqual(100m, acc.Balance);
			acc.SwitchCurrency(Sample.CurrencyUnit.GBP);
			Assert.AreEqual(50m, acc.Balance);
			acc.SwitchCurrency(Sample.CurrencyUnit.USD);
			Assert.AreEqual(80m, acc.Balance);
			acc.SwitchCurrency(Sample.CurrencyUnit.EUR);
			Assert.AreEqual(30m, acc.Balance);
			acc.SwitchCurrency(Sample.CurrencyUnit.EUR);
			Assert.AreEqual(30m, acc.Balance);
			try
			{
				acc.SwitchCurrency(Sample.CurrencyUnit.NULL);
				Assert.Fail("Exception expected.");
			}
			catch (ReplayMockException) { }
			sess.ValidateSession();
		}
Esempio n. 16
0
		public void ScenarioPlayTest()
		{

			using (RecorderManager.NewRecordingSession("test"))
			{

				// Make a callee, not really used but needed to record a constructor call:
				MockingProxy callee = new MockingProxy(typeof(Sample.Account), null, "m1");

				// Push some calls in the recorder:
				MockableCall lastcall;
				CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetConstructor(new Type[] { typeof(Sample.CurrencyUnit) }), null));
				lastcall.SetConstructionResult("acc");
				CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetMethod("Deposit"), null));
				lastcall.SetCallResult();
				CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetMethod("Withdraw"), null));
				lastcall.SetCallResult();
				CurrentRecorder.RecordCall(lastcall = new MockableCall(callee, typeof(Sample.Account).GetProperty("Balance").GetGetMethod(), null));
				lastcall.SetCallResult(10m);
			}

			using (RecorderManager.NewPlayBackSession("test", true))
			{
				// Register types to mock:
				MockService.AddTypeToMock(typeof(Sample.Account));

				// Play scenario:
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				acc.Deposit(100m);
				acc.Withdraw(25m);
				Decimal balance = acc.Balance;

				// Checks:
				Assert.AreEqual(10m, balance); // Does not match the scenario, but the mocking result !
			}
		}
Esempio n. 17
0
		public void PropertyMock()
		{
			using (RecorderManager.NewRecordingSession("test"))
			{
				MockService.AddTypeToMock(typeof(Sample.Account));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				Assert.IsTrue(MockService.IsMock(acc));
				acc.Deposit(100m);
				acc.Withdraw(50m);
				Assert.IsTrue(acc.IsBalancePositive);
			}
			using (RecorderManager.NewPlayBackSession("test", true))
			{
				MockService.AddTypeToMock(typeof(Sample.Account));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				Assert.IsTrue(MockService.IsMock(acc));
				acc.Deposit(100m);
				acc.Withdraw(50m);
				Assert.IsTrue(acc.IsBalancePositive);
			}
		}
Esempio n. 18
0
		public void RecordAndPlaybackTest()
		{
			decimal balanceToBe;
			Assert.AreEqual(RecorderState.None, RecorderManager.Action);
			using (RecorderManager.NewRecordingSession("test"))
			{
				Assert.AreEqual(RecorderState.Recording, RecorderManager.Action);
				MockService.AddTypeToMock(typeof(Sample.Account));
				MockService.AddTypeToMock(typeof(Sample.CustomizableCurrencyService));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				Assert.IsTrue(MockService.IsMock(acc));
				acc.Deposit(100m);
				acc.SwitchCurrency(Sample.CurrencyUnit.USD);
				acc.Withdraw(50m);
				balanceToBe = acc.Balance;
			}
			Assert.AreEqual(RecorderState.None, RecorderManager.Action);
			Assert.AreEqual(5, CurrentRecorder.Count);
			Assert.IsTrue(CurrentRecorder[0].IsConstructorCall);
			Assert.AreEqual("Deposit", CurrentRecorder[1].Method.Name);
			Assert.AreEqual("SwitchCurrency", CurrentRecorder[2].Method.Name);
			Assert.AreEqual("Withdraw", CurrentRecorder[3].Method.Name);
			Assert.AreEqual("get_Balance", CurrentRecorder[4].Method.Name);
			Assert.AreEqual(balanceToBe, CurrentRecorder[4].ReturnValue);
			using (RecorderManager.NewPlayBackSession("test", false))
			{
				Assert.AreEqual(RecorderState.PlayBack, RecorderManager.Action);
				MockService.AddTypeToMock(typeof(Sample.Account));
				MockService.AddTypeToMock(typeof(Sample.CustomizableCurrencyService));
				Sample.Account acc = new Sample.Account(Sample.CurrencyUnit.EUR);
				Assert.IsTrue(MockService.IsMock(acc));
				acc.Deposit(100m);
				acc.SwitchCurrency(Sample.CurrencyUnit.USD);
				acc.Withdraw(50m);
				Assert.AreEqual(balanceToBe, acc.Balance);
				RecorderManager.ValidatePlayBack();
			}
			Assert.AreEqual(RecorderState.None, RecorderManager.Action);
			Assert.IsTrue(CurrentRecorder.Validated);
		}