Beispiel #1
0
        public static MockProxy <TDelegate> BodyBy <TDelegate>(this TypedBehaviorPreparable <TDelegate> target, MockStorage ms) where TDelegate : class
        {
            var m = ms.Create <TDelegate>();

            target.Body = m.Object;
            return(m);
        }
Beispiel #2
0
        public void CreateOfTMockBehavior_can_create_specified_behavior_mock()
        {
            // Arrange
            var ms = new MockStorage(MockBehavior.Default);
            var m  = ms.Create <ICloneable>(MockBehavior.Strict);

            // Act, Assert
            Assert.Throws <MockException>(() => m.Object.Clone());
        }
Beispiel #3
0
        public void CreateOfTObjectArray_can_create_specified_behavior_mock_of_the_type_that_has_constructor_with_parameters()
        {
            // Arrange
            var ms = new MockStorage(MockBehavior.Default);
            var m  = ms.Create <ConstructorWithParameters>(MockBehavior.Strict, 42);

            m.Setup(_ => _.Parse("cba")).Returns(23);

            // Act, Assert
            Assert.Throws <MockException>(() => m.Object.Parse("abc"));
        }
        public async Task InitializeSmtpClientAsync_Successful()
        {
            MockStorage storage = new MockStorage();

            SmtpClientGetter getter = storage.Create();
            await getter.InitializeSmtpClientAsync(storage.SmtpConfigMock.Object);

            Assert.AreSame(getter.Smtp, storage.SmtpClientMock.Object);

            storage.SmtpClientMock.Verify(a =>
                                          a.ConnectAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <SecureSocketOptions>(), It.IsAny <CancellationToken>()), Times.Once);
            storage.SmtpClientMock.Verify(a =>
                                          a.AuthenticateAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once);
            storage.SmtpClientMock.VerifyNoOtherCalls();
        }
        public void Dispose_NotConnected()
        {
            MockStorage storage = new MockStorage();

            storage.SmtpClientMock.Setup(a => a.IsConnected).Returns(false);

            SmtpClientGetter smtpGetter = storage.Create();

            Assert.IsNotNull(smtpGetter.Smtp);

            smtpGetter.Dispose();
            Assert.IsNull(smtpGetter.Smtp);

            storage.SmtpClientMock.VerifyGet(a => a.IsConnected, Times.Once);
            storage.SmtpClientMock.VerifyNoOtherCalls();
        }
Beispiel #6
0
        public void VerifyAll_should_verify_all_setup()
        {
            // Arrange
            var ms = new MockStorage(MockBehavior.Default);
            var m  = ms.Create <IComparable <int> >();

            m.Expect(_ => _.CompareTo(42)).Returns(0);
            m.Setup(_ => _.CompareTo(23)).Returns(1);

            // Act
            m.Object.CompareTo(42);
            m.Object.CompareTo(23);

            // Assert
            ms.VerifyAll();
        }
        public void Dispose_Connected_Successful()
        {
            MockStorage storage = new MockStorage();

            storage.SmtpClientMock.Setup(a => a.IsConnected).Returns(true);

            SmtpClientGetter smtpGetter = storage.Create();

            Assert.IsNotNull(smtpGetter.Smtp);

            smtpGetter.Dispose();
            Assert.IsNull(smtpGetter.Smtp);

            storage.SmtpClientMock.VerifyGet(a => a.IsConnected, Times.Once);
            storage.SmtpClientMock.Verify(a => a.Disconnect(true, It.IsAny <CancellationToken>()), Times.Once);
            storage.SmtpClientMock.VerifyNoOtherCalls();
        }
        public void Dispose_Connected_ConnectionDisposed()
        {
            MockStorage storage = new MockStorage();

            storage.SmtpClientMock.Setup(a => a.IsConnected).Returns(true);

            storage.SmtpClientMock.Setup(a => a.Disconnect(true, It.IsAny <CancellationToken>()))
            .Throws(new ObjectDisposedException(nameof(storage.SmtpClientMock.Object)));

            SmtpClientGetter smtpGetter = storage.Create();

            Assert.IsNotNull(smtpGetter.Smtp);

            smtpGetter.Dispose();
            Assert.IsNull(smtpGetter.Smtp);

            storage.SmtpClientMock.VerifyGet(a => a.IsConnected, Times.Once);
            storage.SmtpClientMock.Verify(a => a.Disconnect(true, It.IsAny <CancellationToken>()), Times.Once);
            storage.SmtpClientMock.VerifyNoOtherCalls();
        }
        public async Task InitializeSmtpClientAsync_ConnectFailed()
        {
            MockStorage storage = new MockStorage();

            Exception originalEx = new Exception();

            storage.SmtpClientMock.Setup(a =>
                                         a.ConnectAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <SecureSocketOptions>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(originalEx);

            SmtpNotConnectedException ex =
                await Assert.ThrowsExceptionAsync <SmtpNotConnectedException>(async() =>
                                                                              await storage.Create().InitializeSmtpClientAsync(storage.SmtpConfigMock.Object));

            Assert.AreSame(originalEx, ex.InnerException);

            storage.SmtpClientMock.Verify(a =>
                                          a.ConnectAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <SecureSocketOptions>(), It.IsAny <CancellationToken>()), Times.Once);
            storage.SmtpClientMock.VerifyNoOtherCalls();
        }
Beispiel #10
0
        public void RestartCurrentProcessWith_should_invoke_additionalSetup()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);
                PEnvironmentMixin.AutoBodyBy(ms);

                var curProcMainMod = new PProxyProcessModule();
                curProcMainMod.AutoBodyBy(ms);

                var procStartInfo = new ProcessStartInfo();
                var curProc = new PProxyProcess();
                curProc.AutoBodyBy(ms);
                curProc.StartInfoGet().Body = @this => procStartInfo;
                curProc.MainModuleGet().Body = @this => curProcMainMod;

                PProcessMixin.AutoBodyBy(ms).
                    Customize(m => m.
                        Do(PProcess.StartProcessStartInfo).Expect(_ => _(It.Is<ProcessStartInfo>(x =>
                            x.Verb == "runas"
                        )), Times.Once()).Returns(new PProxyProcess())
                    );
                PProcess.GetCurrentProcess().Body = () => curProc;

                var additionalSetup = ms.Create<Action<ProcessStartInfo>>().Object;
                Mock.Get(additionalSetup).Setup(_ => _(It.IsAny<ProcessStartInfo>())).Callback(() => procStartInfo.Verb = "runas");


                // Act
                var result = ULProcessMixin.RestartCurrentProcessWith(additionalSetup);


                // Assert
                Assert.IsTrue(result);
                ms.Verify();
            }
        }
Beispiel #11
0
        public void RestartCurrentProcessWith_should_invoke_additionalSetup()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);
                PEnvironmentMixin.AutoBodyBy(ms);

                var curProcMainMod = new PProxyProcessModule();
                curProcMainMod.AutoBodyBy(ms);

                var procStartInfo = new ProcessStartInfo();
                var curProc = new PProxyProcess();
                curProc.AutoBodyBy(ms);
                curProc.StartInfoGet().Body = @this => procStartInfo;
                curProc.MainModuleGet().Body = @this => curProcMainMod;

                PProcessMixin.AutoBodyBy(ms).
                    Customize(m => m.
                        Do(PProcess.StartProcessStartInfo).Expect(_ => _(It.Is<ProcessStartInfo>(x =>
                            x.Verb == "runas"
                        )), Times.Once()).Returns(new PProxyProcess())
                    );
                PProcess.GetCurrentProcess().Body = () => curProc;

                var additionalSetup = ms.Create<Action<ProcessStartInfo>>().Object;
                Mock.Get(additionalSetup).Setup(_ => _(It.IsAny<ProcessStartInfo>())).Callback(() => procStartInfo.Verb = "runas");

                // Act
                var result = ULProcessMixin.RestartCurrentProcessWith(additionalSetup);

                // Assert
                Assert.IsTrue(result);
                ms.Verify();
            }
        }