Exemple #1
0
        public void CanMockMethodWithOutParamOnInterface()
        {
            // InterfaceOnlyMockObjectFactory-generated mocks do not currently support this
            // - They expect out params to be explicitly set

            AssertCanMockMethodWithOutParam(Mocks.CreateMock <IGenericOutParamInterface>());
        }
Exemple #2
0
        public void WriteObject_ByteArrays_WrittenInSegments(AMFObjectEncoding objectEncoding, byte[] expected, byte[] bytes)
        {
            IASValue byteArray = Mocks.CreateMock <IASValue>();

            byteArray.AcceptVisitor(serializer, null);
            LastCall.IgnoreArguments().Do((AcceptVisitorDelegate) delegate(IActionScriptSerializer theSerializer, IASValueVisitor visitor)
            {
                ArraySegment <byte>[] segments = new ArraySegment <byte> [bytes.Length];
                for (int i = 0; i < bytes.Length; i++)
                {
                    segments[i] = new ArraySegment <byte>(bytes, i, 1);
                }

                visitor.VisitByteArray(serializer, bytes.Length, segments);
            });

            Mocks.ReplayAll();

            output.ObjectEncoding = objectEncoding;
            output.BeginObjectStream();
            output.WriteObject(byteArray);
            output.EndObjectStream();

            CollectionAssert.AreElementsEqual(expected, stream.ToArray());
        }
Exemple #3
0
        public void WriteObject_Objects_Externalizable_AMF3()
        {
            IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>();

            externalizableValue.WriteExternal(output);
            LastCall.Do((WriteExternalDelegate) delegate(IDataOutput outputToUse)
            {
                // Note: outputToUse will be the same instance as output which we've already
                // tested so we don't need to try all combinations here.  Just a few as a sanity check.
                outputToUse.WriteUTF("abc");
                outputToUse.WriteInt(10);
                outputToUse.WriteObject(new ASString("def"));
            });

            ASClass @class             = new ASClass("class", ASClassLayout.Externalizable, EmptyArray <string> .Instance);
            ASExternalizableObject obj = new ASExternalizableObject(@class, externalizableValue);

            Mocks.ReplayAll();

            output.ObjectEncoding = AMFObjectEncoding.AMF3;
            output.BeginObjectStream();
            output.WriteObject(obj);
            output.EndObjectStream();

            byte[] expected = new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data, (byte)AMF3ObjectTypeCode.Object, 0x07,
                                           0x0b, 0x63, 0x6c, 0x61, 0x73, 0x73,                     // class def
                                           0x00, 0x03, 0x61, 0x62, 0x63,                           // write utf "abc"
                                           0x00, 0x00, 0x00, 0x0a,                                 // write int 10
                                           (byte)AMF3ObjectTypeCode.String, 0x07, 0x64, 0x65, 0x66 // write object "def"
            };

            CollectionAssert.AreElementsEqual(expected, stream.ToArray());
        }
Exemple #4
0
        public void UnexpectedInvocationExceptionIsClearedAfterBeingThrownInVerify()
        {
            Mock <IHelloWorld> mock = Mocks.CreateMock <IHelloWorld>();

            try
            {
                mock.MockObject.Ahh();
            }
            catch (UnexpectedInvocationException)
            {
                // evil code >:-]
            }

            try
            {
                // Exception should be initially rethrown here...
                Mocks.VerifyAllExpectationsHaveBeenMet();
            }
            catch (UnexpectedInvocationException)
            {
            }

            // It should not be rethrown again...
            Mocks.VerifyAllExpectationsHaveBeenMet();
        }
        public void CanMockClassWithProtectedInternalMembers()
        {
            Mock <VisibilityTestClass.SomeClassWithProtectedInternalMembers> mock = Mocks.CreateMock <VisibilityTestClass.SomeClassWithProtectedInternalMembers>();

            mock.Expects.One.Method(_ => _.DoWork());
            mock.MockObject.DoWork();
        }
Exemple #6
0
        public override void SetUp()
        {
            base.SetUp();

            externalizableClass = new ASClass("extern", ASClassLayout.Externalizable, EmptyArray <string> .Instance);
            externalizable      = Mocks.CreateMock <IExternalizable>();
        }
        public void CanMockInternalInterface()
        {
            Mock <VisibilityTestClass.ISomeInternalInterface> mock = Mocks.CreateMock <VisibilityTestClass.ISomeInternalInterface>();

            mock.Expects.One.Method(_ => _.DoWork());
            mock.MockObject.DoWork();
        }
        public override void SetUp()
        {
            base.SetUp();

            kernel  = Mocks.CreateMock <IKernel>();
            factory = new WindsorJobFactory(kernel);
        }
        public void MockServiceModelIClientChannel()
        {
            Mock <IClientChannel> c = Mocks.CreateMock <IClientChannel>();

            c.Expects.One.Method(_ => _.Open());
            c.MockObject.Open();
        }
Exemple #10
0
        public void SettingExpectationOnNonVirtualOverloadOfVirtualMethodThrowsArgumentException()
        {
            Mock <SampleClassWithVirtualAndNonVirtualMethods> mock = Mocks.CreateMock <SampleClassWithVirtualAndNonVirtualMethods>();

            Expect.That(() => mock.Expects.One.MethodWith(_ => _.Add(1, 2, 3)).WillReturn(10)).Throws <ArgumentException>();

            Mocks.ClearExpectations();
        }
Exemple #11
0
        public void SettingExpectationOnNonVirtualMethodThrowsArgumentException()
        {
            Mock <SampleClassWithVirtualAndNonVirtualMethods> mock = Mocks.CreateMock <SampleClassWithVirtualAndNonVirtualMethods>("myMock");

            // The target method is non-virtual, so we expect an exception.
            // (we use 'Never' here to avoid an undesired failure in teardown).
            mock.Expects.No.Method(_ => _.Subtract(0, 0)).With(2, 1).Will(Return.Value(10));
        }
Exemple #12
0
        public void CanSetExpectationOnVirtualMethod()
        {
            Mock <SampleClassWithVirtualAndNonVirtualMethods> mock = Mocks.CreateMock <SampleClassWithVirtualAndNonVirtualMethods>();

            // Virtual target method
            mock.Expects.One.Method(_ => _.Add(0, 0)).With(1, 2).Will(Return.Value(10));
            Assert.AreEqual(10, mock.MockObject.Add(1, 2));
        }
Exemple #13
0
        public void EventAdd()
        {
            Mock <ISyntacticSugar> sugar = Mocks.CreateMock <ISyntacticSugar>("sugar");

            sugar.Expects.One.EventBinding(s => s.Actions -= null);

            sugar.MockObject.Actions += DoAction;
        }
        public void CorrectMessageWhenReturnValueNotSet()
        {
            Mock <IParentInterface> myHelloWorld = Mocks.CreateMock <IParentInterface>();

            myHelloWorld.Expects.One.Method(_ => _.Method(0)).WithAnyArguments();             //.Will(Return.Value(true));//, new SetNamedParameterAction("number", 3)); //.With();

            Expect.That(() => myHelloWorld.MockObject.Method(3)).Throws <IncompleteExpectationException>("You have to set the return value for method 'IsPrime' on 'IMyHelloWorld' mock.");
        }
Exemple #15
0
        public void CanMakeMultipleCallsToImplementingWhenCreatingMock()
        {
            Mock <IMockedType> mock = Mocks.CreateMock <IMockedType>(DefinedAs.Implementing <IEnumerable>().Implementing <IDisposable>());

            Assert.IsTrue(typeof(IMockedType).IsInstanceOfType(mock.MockObject));
            Assert.IsTrue(typeof(IEnumerable).IsInstanceOfType(mock.MockObject));
            Assert.IsTrue(typeof(IDisposable).IsInstanceOfType(mock.MockObject));
        }
Exemple #16
0
        public override void SetUp()
        {
            base.SetUp();

            stream     = new MemoryStream();
            serializer = Mocks.CreateMock <IActionScriptSerializer>();
            input      = new AMFDataInput(stream, serializer);
        }
Exemple #17
0
        public void CanSetExpectationOnMethodOnMockedGenericClass()
        {
            Mock <GenericClass <string> > mock = Mocks.CreateMock <GenericClass <string> >();

            mock.Expects.One.Method(_ => _.GetT()).Will(Return.Value("ABC"));

            Assert.AreEqual("ABC", mock.MockObject.GetT());
        }
Exemple #18
0
        public void CanSetExpectationOnGenericMethodWithConstraintOnMockedClass()
        {
            Mock <SampleClassWithGenericMethods> mock = Mocks.CreateMock <SampleClassWithGenericMethods>();

            mock.Expects.One.Method(_ => _.GetCount(new[] { 0 })).WithAnyArguments().Will(Return.Value(3));

            Assert.AreEqual(3, mock.MockObject.GetCount(new int[5]));
        }
Exemple #19
0
        public void CanSetExpectationOnGenericMethodOnMockedClass()
        {
            Mock <SampleClassWithGenericMethods> mock = Mocks.CreateMock <SampleClassWithGenericMethods>();

            mock.Expects.One.Method(_ => _.GetStringValue("123")).WithAnyArguments().Will(Return.Value("ABC"));

            Assert.AreEqual("ABC", mock.MockObject.GetStringValue("XYZ"));
        }
Exemple #20
0
        public void CanSetExpectationOnAbstractMethodOfClassMock()
        {
            Mock <SampleAbstractClass> mock = Mocks.CreateMock <SampleAbstractClass>();

            mock.Expects.One.Method(_ => _.Add(0, 0)).WithAnyArguments().Will(Return.Value(7));

            Assert.AreEqual(7, mock.MockObject.Add(1, 2));
        }
        public void GetJobCallsResolve()
        {
            IJob job = Mocks.CreateMock <IJob>();

            Expect.Call(kernel.Resolve("job.key", typeof(IJob))).Return(job);
            Mocks.ReplayAll();

            Assert.AreSame(job, factory.GetJob("job.key"));
        }
Exemple #22
0
        public void MockObjectsMayBePlacedIntoServiceContainers()
        {
            var container  = new ServiceContainer();
            var mockedType = Mocks.CreateMock <IMockedType>();

            container.AddService(typeof(IMockedType), mockedType.MockObject);

            Assert.AreSame(mockedType.MockObject, container.GetService(typeof(IMockedType)));
        }
Exemple #23
0
        public void CanHaveClassWithIMockObjectMembers()
        {
            Mock <SampleClassWithIMockObjectMembers> mock = Mocks.CreateMock <SampleClassWithIMockObjectMembers>();

            mock.Expects.One.Method(_ => _.Multiply(0, 0)).WithAnyArguments().Will(Return.Value(133));
            int result = mock.MockObject.Multiply(12, 12);

            //// even if 12 by 12 is 144, we mocked the method with 133:
            Assert.AreEqual(133, result, "Mock wasn't created successful.");
        }
Exemple #24
0
        public void SetPropertiesWithInitializedInstanceThrows()
        {
            IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>();

            Mocks.ReplayAll();

            ASExternalizableObject obj = new ASExternalizableObject(externalizableClass, externalizableValue);

            obj.SetProperties(externalizableValue);
        }
Exemple #25
0
        public override void Setup()
        {
            base.Setup();

            myEnumerable = Mocks.CreateMock <IMyEnumerable>();

            data = new[] { "a", "b", "c", "d", "e" };

            myEnumerable.Stub.Out.Method(_ => _.GetEnumerator()).Will(new CallGetEnumeratorAction(data));
        }
Exemple #26
0
        public override void SetUp()
        {
            base.SetUp();

            scheduler = Mocks.CreateMock <IScheduler>();
            logger    = Mocks.CreateMock <ILogger>();
            jobSpec   = new JobSpec("abc", "some job", "with.this.key", PeriodicTrigger.CreateDailyTrigger(DateTime.UtcNow));
            jobData   = new JobData();
            Mocks.ReplayAll();
        }
        public void Logger_GetterAndSetter()
        {
            Mocks.ReplayAll();

            Assert.AreSame(NullLogger.Instance, jobStore.Logger);

            ILogger mockLogger = Mocks.CreateMock <ILogger>();

            jobStore.Logger = mockLogger;
            Assert.AreSame(mockLogger, jobStore.Logger);
        }
        public void StubsCanBeCalledAnyNumberOfTimes()
        {
            Mock <IHelloWorld> helloWorld = Mocks.CreateMock <IHelloWorld>();

            helloWorld.Stub.Out.Method(_ => _.Hello());

            for (int i = 0; i < ANY_NUMBER; i++)
            {
                helloWorld.MockObject.Hello();
            }
        }
Exemple #29
0
        public void CanSetExpectationOnMethodWhenAtLeastOneMatchedOverloadIsVirtualOrAbstract()
        {
            Mock <SampleClassWithVirtualAndNonVirtualMethods> mock = Mocks.CreateMock <SampleClassWithVirtualAndNonVirtualMethods>();

            // Three possible matches here...
            mock.Expects.One.Method(_ => _.Add(0, 0)).WithAnyArguments().Will(Return.Value(10));
            mock.Expects.One.Method(_ => _.Add(0m, 0m)).WithAnyArguments().Will(Return.Value(10m));
            Assert.AreEqual(10, mock.MockObject.Add(1, 2), "Virtual method expectation failed");
            Assert.AreEqual(10, mock.MockObject.Add(1.1m, 2.1m), "Abstract method expectation failed");
            Assert.AreEqual(6, mock.MockObject.Add(1, 2, 3), "Expected call to non-virtual method to go to implementation");
        }
Exemple #30
0
        public void CanSetExpectationOnAbstractMethod()
        {
            Mock <SampleClassWithVirtualAndNonVirtualMethods> mock = Mocks.CreateMock <SampleClassWithVirtualAndNonVirtualMethods>();

            // Abstract target method
            mock.Expects.One.Method(_ => _.Add(1.3m, 2.4m)).With(1.1m, 2.1m).Will(Return.Value(10m));
            Assert.AreEqual(10, mock.MockObject.Add(1.1m, 2.1m));

            mock.Expects.One.MethodWith(_ => _.Add(1.1m, 2.1m)).WillReturn(10);
            Assert.AreEqual(10, mock.MockObject.Add(1.1m, 2.1m));
        }