public void Serialization01Test()
        {
            MemoryStream buffer    = new MemoryStream();
            IFormatter   formatter = new global::System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            // Create some call:
            ArrayList result = new ArrayList();

            result.Add("One");
            result.Add("Two");
            MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1");
            MockableCall call   = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeMethodWithInsAndOuts"), new object[] { 1, 2, null, 3 });

            call.SetCallResult(result, new object[] { 7, 13 });

            // Serialize it:
            formatter.Serialize(buffer, call);
            buffer.Flush();

            // Deserialize it:
            buffer.Position = 0;
            MockableCall deserializedCall = (MockableCall)formatter.Deserialize(buffer);

            // Test result:
            Assert.IsNotNull(deserializedCall);
            Assert.IsFalse(deserializedCall.IsConstructorCall);
            Assert.IsNotNull(deserializedCall.Method);
            Assert.AreEqual("SomeMethodWithInsAndOuts", deserializedCall.Method.Name);
            Assert.AreEqual("Two", ((ArrayList)deserializedCall.ReturnValue)[1]);
            Assert.AreEqual(3, deserializedCall.InArgs[2]);
            Assert.AreEqual(13, deserializedCall.OutArgs[1]);
            Assert.IsTrue(deserializedCall.IsCompleted);
        }
        public void Serialization05Test()
        {
            MemoryStream buffer = new MemoryStream();

            Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter();
            formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple;

            // Create some call:
            MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1");
            MockableCall call   = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeVoid"), new object[] { });

            call.SetCallResult();

            // Serialize it:
            formatter.Serialize(buffer, call);
            buffer.Flush();

            // Deserialize it:
            buffer.Position = 0;
            MockableCall deserializedCall = (MockableCall)formatter.Deserialize(buffer);

            // Test result:
            Assert.IsNotNull(deserializedCall);
            Assert.IsFalse(deserializedCall.IsConstructorCall);
            Assert.IsNotNull(deserializedCall.Method);
            Assert.AreEqual("SomeVoid", deserializedCall.Method.Name);
            Assert.IsTrue(deserializedCall.ReturnValue == null);
            Assert.IsTrue(deserializedCall.IsCompleted);
        }
        /// <summary>
        /// Process synchronous messages through the sink chain. Mocking can be applied here.
        /// </summary>
        public IMessage SyncProcessMessage(IMessage msg)
        {
            IMethodCallMessage   mcm = (IMethodCallMessage)msg;
            IMethodReturnMessage result;

            if (RecorderManager.IsPlaying && RemotingMockService.IsUriToMock(mcm.Uri))
            {
                MockingProxy proxy = new MockingProxy(Type.GetType(mcm.TypeName), new RemotingPlayBackMocker(), mcm.Uri);
                result = (IMethodReturnMessage)proxy.Invoke(msg);
            }
            else if (RecorderManager.IsRecording && !RecorderManager.IsInCall && RemotingMockService.IsUriToMock(mcm.Uri))
            {
                MockingProxy proxy = new MockingProxy(Type.GetType(mcm.TypeName), null, mcm.Uri);
                MockableCall call  = new MockableCall(proxy, mcm);
                using (new RecorderManager.Lock()) {
                    result = SyncProcessMessageOnServer(mcm);
                    call.SetResult(result);
                    RecorderManager.RecordCall(call);
                }
            }
            else
            {
                result = (IMethodReturnMessage)nextMessageSink.SyncProcessMessage(msg);
            }
            return(result);
        }
        public void Serialization04Test()
        {
            MemoryStream buffer = new MemoryStream();

            Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter();
            formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple;

            // Create some call:
            MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1");
            MockableCall call   = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("IsItTrue"), new object[] { true });

            call.SetCallResult(true);

            // Serialize it:
            formatter.Serialize(buffer, call);
            buffer.Flush();

            // Deserialize it:
            buffer.Position = 0;
            MockableCall deserializedCall = (MockableCall)formatter.Deserialize(buffer);

            // Test result:
            Assert.IsNotNull(deserializedCall);
            Assert.IsFalse(deserializedCall.IsConstructorCall);
            Assert.IsNotNull(deserializedCall.Method);
            Assert.AreEqual("IsItTrue", deserializedCall.Method.Name);
            Assert.IsFalse(deserializedCall.ReturnValue is string, "ReturnValue of type bool was read back through soap serialization as a string.");
            Assert.IsTrue(deserializedCall.ReturnValue is bool);
            Assert.IsTrue(deserializedCall.InArgs[0] is bool);
            Assert.IsTrue((bool)deserializedCall.ReturnValue);
            Assert.IsTrue(deserializedCall.IsCompleted);
        }
示例#5
0
        public void PlayBackCall(MockableCall actualCall)
        {
            MockableCall expectedCall = (MockableCall)this.records[this.pointer++];

            Assert.AreEqual(expectedCall.MethodSignature, actualCall.MethodSignature);
            actualCall.SetResult(expectedCall);
        }
        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 !
            }
        }
示例#7
0
 /// <summary>
 /// Validates a call before replaying it. Checks that the call matches the expected
 /// call. If not so, raises a ReplayMockException.
 /// </summary>
 public virtual void Validate(MockableCall call)
 {
     if (!this.methodName.Equals(call.Method.Name))
     {
         throw new ReplayMockException(call, "Expected call name does not match received call.\r\nCall expected: " + methodName + "\r\nCall received: " + call.Method.Name);
     }
 }
示例#8
0
 /// <summary>
 /// Implements IMocker.
 /// </summary>
 public void HandleCall(MockingProxy proxy, MockableCall call)
 {
     try {
         IExpectedCall expectedCall = DequeueExpectedCall();
         expectedCall.Replay(call);
     } catch (ArgumentOutOfRangeException) {
         throw new ReplayMockException(call, "Call not expected.");
     }
 }
        public void GetParametersTest()
        {
            // Create call:
            MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1");
            MockableCall call   = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeMethodWithInsAndOuts"), new object[] { 1, 2, null, 3 });

            // Check parameters:
            Assert.AreEqual(3, call.GetInParameters().Length, "Input parameters count mismatch.");
            Assert.AreEqual(2, call.GetOutParameters().Length, "Output parameters count mismatch.");
        }
示例#10
0
        /// <summary>
        /// Replays the expected call and checks its arguments.
        /// </summary>
        public void Replay(MockableCall call)
        {
            // Check argument count:
            if (arguments.Length != call.Method.GetParameters().Length)
            {
                throw new ReplayMockException(call, "Call to method \"" + call.Method.Name + "\" expected wrong number of arguments.");
            }
            // Check arguments individually:
            int i = -1;

            foreach (System.Reflection.ParameterInfo pinfo in call.GetInParameters())
            {
                i++;
                if (pinfo.IsOut)
                {
                    continue;                     // Skip output parameters
                }
                if ((arguments[pinfo.Position] == null) && (call.InArgs[i] == null))
                {
                    continue;                     // OK if both NULL
                }
                if ((arguments[pinfo.Position] == null) || (call.InArgs[i] == null))
                {
                    throw new ReplayMockException(call, "Argument \"" + pinfo.Name + "\" of method \"" + call.MethodSignature + "\" has a different value than expected.");
                }
                if (arguments[pinfo.Position] is IExpectedValue)
                {
                    if ((arguments[pinfo.Position] as IExpectedValue).MatchesExpectation(call.InArgs[i]))
                    {
                        continue;
                    }
                    else
                    {
                        throw new ReplayMockException(call, "Argument \"" + pinfo.Name + "\" of method \"" + call.MethodSignature + "\" has a different value than expected.");
                    }
                }
                if (!arguments[pinfo.Position].Equals(call.InArgs[i]))
                {
                    throw new ReplayMockException(call, "Argument \"" + pinfo.Name + "\" of method \"" + call.MethodSignature + "\" has a different value than expected.");
                }
            }
            // If all passed, replay the call:
            innerCall.Replay(call);
        }
示例#11
0
 public void HandleCall(MockingProxy proxy, MockableCall call)
 {
     if (call.IsConstructorCall)
     {
         call.SetConstructionResult("CurrencyMock");
     }
     else
     {
         if (call.Method.Name.Equals("ConvertAmount"))
         {
             // Retrieve call args:
             decimal amount = (decimal)call.InArgs[0];
             // Mock the call:
             call.SetCallResult(amount);
         }
         else
         {
             // Return from a void call:
             call.SetCallResult();
         }
     }
 }
        public void IsInOutParameterTest()
        {
            // Create call:
            MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1");
            MockableCall call   = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeMethodWithInsAndOuts"), new object[] { 1, 2, null, 3 });

            // Check in parameters:
            foreach (global::System.Reflection.ParameterInfo param in call.GetInParameters())
            {
                Assert.IsTrue(call.IsParameterIn(param.Position));
            }
            // Check out parameters:
            foreach (global::System.Reflection.ParameterInfo param in call.GetOutParameters())
            {
                Assert.IsTrue(call.IsParameterOut(param.Position));
            }
            // All params must be IN, OUT or IN/OUT:
            foreach (global::System.Reflection.ParameterInfo param in call.Method.GetParameters())
            {
                Assert.IsTrue(call.IsParameterIn(param.Position) || call.IsParameterOut(param.Position));
            }
        }
        public void Serialization03Test()
        {
            // Create and initialize formatter:
            Sfmt.Soap.SoapFormatter formatter = new Sfmt.Soap.SoapFormatter();
            formatter.AssemblyFormat = Sfmt.FormatterAssemblyStyle.Simple;

            // Create DS:
            global::System.Data.DataSet    ds  = new global::System.Data.DataSet();
            global::System.Data.DataTable  dt  = ds.Tables.Add("SomeTable");
            global::System.Data.DataColumn dc1 = dt.Columns.Add("ID", typeof(global::System.Int32));
            ds.AcceptChanges();

            // Create MockableCall:
            MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1");
            MockableCall call   = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeMethodWithInsAndOuts"), new object[] { 1, 2, null, 3 });

            // Set dataset as callresult:
            call.SetCallResult(ds);

            Assert.IsNotNull(call.ReturnValue, "Test setup failure, test could not even be run !");

            // Serialize call:
            MemoryStream buffer = new MemoryStream();

            formatter.Serialize(buffer, call);

            // Reset buffer:
            buffer.Flush();
            buffer.Position = 0;

            // Deserialize call:
            call = (MockableCall)formatter.Deserialize(buffer);

            // Verify results (expect returnValue to be non-null):
            Assert.IsNotNull(call);
            Assert.IsNotNull(call.ReturnValue, "ReturnValue is null, the old implementation issue has reoccured...");
            Assert.IsTrue(call.ReturnValue is global::System.Data.DataSet, "What the heck ? returnValue should have been a dataset !");
        }
 /// <summary>
 /// Process asynchronous messages through the sink chain. Mocking can be applied here.
 /// </summary>
 public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
 {
     if (RecorderManager.IsPlaying)
     {
         MockableCall     call    = new MockableCall(null, (IMethodCallMessage)msg);
         AsyncCallHandler handler = new AsyncCallHandler(replySink, call);
         RecorderManager.RecordCall(call);
     }
     else if (RecorderManager.IsRecording)
     {
     }
     else
     {
         return(NextSink.AsyncProcessMessage(msg, replySink));
     }
     return(nextMessageSink.AsyncProcessMessage(
                msg,
                new AsyncCallHandler(
                    replySink,
                    new MockableCall(null, (IMethodCallMessage)msg)
                    )
                ));
 }
 /// <summary>
 /// Handles the call by playing it back.
 /// </summary>
 public void HandleCall(MockingProxy proxy, MockableCall call)
 {
     RecorderManager.PlayBackCall(call);
 }
示例#16
0
		/// <summary>
		/// Handles the call by playing it back.
		/// </summary>
		public void HandleCall(MockingProxy proxy, MockableCall call) {
			RecorderManager.PlayBackCall(call);
		}
示例#17
0
 public void RecordCall(MockableCall call)
 {
     this.records.Add(call);
 }
示例#18
0
 /// <summary>
 /// Replays the expected call by returning the expected returnvalue.
 /// </summary>
 public override void Replay(MockableCall call)
 {
     base.Replay(call);
     call.SetCallResult(returnValue, outArgs);
 }
 public void CreationTest()
 {
     MockingProxy callee = new MockingProxy(typeof(Sample.FooBar), null, "m1");
     MockableCall call   = new MockableCall(callee, typeof(Sample.FooBar).GetMethod("SomeMethodWithInsAndOuts"), new object[] { 1, 2, null, 3 });
 }
 public AsyncCallHandler(IMessageSink next, MockableCall call)
 {
     this.next = next;
     this.call = call;
 }
示例#21
0
 /// <summary>
 /// Replays the expected call.
 /// </summary>
 public virtual void Replay(MockableCall call)
 {
     Validate(call);
 }
示例#22
0
 /// <summary>
 /// Replays the expected call by throwing the exception.
 /// </summary>
 public override void Replay(MockableCall call)
 {
     base.Replay(call);
     call.SetExceptionResult(exception);
 }