コード例 #1
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 !
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        private PrivateAccessor(object instance, Type type)
        {
            ProfilerInterceptor.GuardInternal(() =>
            {
                if (instance != null)
                {
#if (!COREFX && !NETCORE)
                    var realProxy = MockingProxy.GetRealProxy(instance);
                    if (realProxy != null)
                    {
                        instance = realProxy.WrappedInstance;
                    }
#endif
                    type = instance.GetType();
                }
                if (type.IsProxy() && type.BaseType != typeof(object))
                {
                    type = type.BaseType;
                }

#if (!PORTABLE && !LITE_EDITION)
                Mock.Intercept(type);
#endif
            });

            this.instance = instance;
            this.type     = type;
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
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.");
     }
 }
コード例 #8
0
        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.");
        }
コード例 #9
0
 /// <summary>
 /// Creates and returns a mocker for the given object type.
 /// </summary>
 /// <param name="type">The object type to mock. This can be either an interface, or a type derived from MarshalByRef.</param>
 /// <param name="name">The name uniquely identifying the instance.</param>
 public virtual ManualMocker Mock(Type type, string name)
 {
     try {
         ManualMocker mocker = new ManualMocker();
         MockingProxy proxy  = new MockingProxy(type, mocker, name);
         mocker.Initialize(proxy);
         mockers.Add(mocker);
         return(mocker);
     } catch (ArgumentException ex) {
         throw new MockException("Type to mock must be an interface or derive from MarshalByRef.", ex);
     }
 }
コード例 #10
0
        private bool CompareValueTo(IMatcher other)
        {
            var valueMatcher = other as IValueMatcher;

            if (valueMatcher == null)
            {
                return(false);
            }

            if (this.IsValueType)
            {
                return(Equals(this.reference, valueMatcher.Value));
            }
            return(ReferenceEquals(MockingProxy.Unwrap(this.reference), MockingProxy.Unwrap(valueMatcher.Value)));
        }
コード例 #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();
         }
     }
 }
コード例 #12
0
        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));
            }
        }
コード例 #13
0
        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 !");
        }
コード例 #14
0
 /// <summary>
 /// Handles the call by playing it back.
 /// </summary>
 public void HandleCall(MockingProxy proxy, MockableCall call)
 {
     RecorderManager.PlayBackCall(call);
 }
コード例 #15
0
		/// <summary>
		/// Handles the call by playing it back.
		/// </summary>
		public void HandleCall(MockingProxy proxy, MockableCall call) {
			RecorderManager.PlayBackCall(call);
		}
コード例 #16
0
 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 });
 }
コード例 #17
0
 internal void Initialize(MockingProxy proxy)
 {
     this.proxy = proxy;
 }