Beispiel #1
0
        public void CallWebService_should_set_HttpWebRequest_to_request_textxml_content()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);

                // And also, you can verify whether HttpWebRequest is set intendedly if you use Moq.
                var requestProxy = new PProxyHttpWebRequest();
                requestProxy.ContentTypeSetString().BodyBy(ms).Expect(_ => _(requestProxy, "text/xml;charset=\"utf-8\""));
                requestProxy.MethodSetString().BodyBy(ms).Expect(_ => _(requestProxy, "GET"));
                requestProxy.TimeoutSetInt32().BodyBy(ms).Expect(_ => _(requestProxy, 1000));
                requestProxy.CredentialsSetICredentials().BodyBy(ms).Expect(_ => _(requestProxy, CredentialCache.DefaultNetworkCredentials));

                var responseProxy = new PProxyHttpWebResponse();
                responseProxy.StatusCodeGet().Body = @this => HttpStatusCode.OK;
                requestProxy.GetResponse().Body    = @this => responseProxy;

                var url = "testService";
                PWebRequest.CreateString().Body = @this => requestProxy;


                // Act
                var actual = new WebServiceClient().CallWebService(url);


                // Assert
                Assert.IsTrue(actual);
                ms.Verify();
            }
        }
        public void CallWebService_should_return_false_if_HttpStatusCode_is_Forbidden()
        {
            using (new IndirectionsContext())
            {
                // Arrange 
                var ms = new MockStorage(MockBehavior.Strict);

                var requestProxy = new PProxyHttpWebRequest();
                requestProxy.ExcludeGeneric().DefaultBehavior = IndirectionBehaviors.DefaultValue;

                var responseProxy = new PProxyHttpWebResponse();
                responseProxy.StatusCodeGet().Body = @this => HttpStatusCode.Forbidden;
                requestProxy.GetResponse().Body = @this => responseProxy;

                // To improve robustness against unintended modification, you should verify inputs to side-effects by Moq.
                // For example, the original test will pass even if you unintendedly changed the original production code as follows: 
                // var request = CreateWebRequest(url);
                //   -> var request = CreateWebRequest("Foo");
                var url = "testService";
                PWebRequest.CreateString().BodyBy(ms).Expect(_ => _(url)).Returns(requestProxy);


                // Act 
                var actual = new WebServiceClient().CallWebService(url);


                // Assert
                Assert.IsFalse(actual);
                ms.Verify();
            }
        }
Beispiel #3
0
        public void CallWebService_should_return_false_if_HttpStatusCode_is_Forbidden()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);

                var requestProxy = new PProxyHttpWebRequest();
                requestProxy.ExcludeGeneric().DefaultBehavior = IndirectionBehaviors.DefaultValue;

                var responseProxy = new PProxyHttpWebResponse();
                responseProxy.StatusCodeGet().Body = @this => HttpStatusCode.Forbidden;
                requestProxy.GetResponse().Body    = @this => responseProxy;

                // To improve robustness against unintended modification, you should verify inputs to side-effects by Moq.
                // For example, the original test will pass even if you unintendedly changed the original production code as follows:
                // var request = CreateWebRequest(url);
                //   -> var request = CreateWebRequest("Foo");
                var url = "testService";
                PWebRequest.CreateString().BodyBy(ms).Expect(_ => _(url)).Returns(requestProxy);


                // Act
                var actual = new WebServiceClient().CallWebService(url);


                // Assert
                Assert.IsFalse(actual);
                ms.Verify();
            }
        }
Beispiel #4
0
        public void MockStorage_should_provide_fluent_setup_through_MockProxy()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);
                PProcess.StartStringString().BodyBy(ms).Expect(_ => _("file name", "arguments")).Returns(Process.GetCurrentProcess());

                // Act
                var proc = Process.Start("file name", "arguments");

                // Assert
                Assert.AreEqual(Process.GetCurrentProcess().Id, proc.Id);
                ms.Verify();
            }
        }
Beispiel #5
0
        public void Verify_should_verify_only_verifiable_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(24);

            // Assert
            ms.Verify();
        }
Beispiel #6
0
        public void RestartCurrentProcess_should_start_new_process_and_close_current_process()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);
                PEnvironment.GetCommandLineArgs().BodyBy(ms).Expect(_ => _(), Times.Once()).Returns(new[] { "file name" });
                PEnvironment.CurrentDirectoryGet().BodyBy(ms).Expect(_ => _(), Times.Once()).Returns("current directory");

                var curProcMainMod = new PProxyProcessModule();
                curProcMainMod.AutoBodyBy(ms).
                Customize(m => m.
                          Do(curProcMainMod.FileNameGet).Expect(_ => _(curProcMainMod), Times.Once()).Returns("file path")
                          );

                var curProc = new PProxyProcess();
                curProc.StartInfoGet().BodyBy(ms).Expect(_ => _(curProc), Times.Once()).Returns(new ProcessStartInfo()
                {
                    UserName = "******"
                });
                curProc.MainModuleGet().BodyBy(ms).Expect(_ => _(curProc), Times.Once()).Returns(curProcMainMod);
                curProc.CloseMainWindow().BodyBy(ms).Expect(_ => _(curProc), Times.Once()).Returns(true);

                PProcess.GetCurrentProcess().BodyBy(ms).Expect(_ => _(), Times.Once()).Returns(curProc);
                PProcess.StartProcessStartInfo().BodyBy(ms).Expect(_ => _(It.Is <ProcessStartInfo>(x =>
                                                                                                   x.UserName == "urasandesu" &&
                                                                                                   x.UseShellExecute == true &&
                                                                                                   x.WorkingDirectory == "current directory" &&
                                                                                                   x.FileName == "file path"
                                                                                                   )), Times.Once()).Returns(new PProxyProcess());


                // Act
                var result = ULProcessMixin.RestartCurrentProcess();


                // Assert
                Assert.IsTrue(result);
                ms.Verify();
            }
        }
Beispiel #7
0
        public void MockStorage_should_provide_fluent_setup_through_itself()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);
                PProcessMixin.AutoBodyBy(ms);
                ms.Customize(c => c.
                             Do(PProcess.StartProcessStartInfo).Expect(_ => _(It.Is <ProcessStartInfo>(x =>
                                                                                                       x.Arguments == "\"arg ument1\" \"argume nt2\""
                                                                                                       ))).Returns(new PProxyProcess())
                             );

                // Act
                var proc = Process.Start(new ProcessStartInfo(Guid.NewGuid().ToString(), "\"arg ument1\" \"argume nt2\""));

                // Assert
                Assert.IsNotNull(proc);
                ms.Verify();
            }
        }
Beispiel #8
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 #9
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 #10
0
        public void RestartCurrentProcess_should_start_new_process_and_close_current_process()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var ms = new MockStorage(MockBehavior.Strict);
                PEnvironment.GetCommandLineArgs().BodyBy(ms).Expect(_ => _(), Times.Once()).Returns(new[] { "file name" });
                PEnvironment.CurrentDirectoryGet().BodyBy(ms).Expect(_ => _(), Times.Once()).Returns("current directory");

                var curProcMainMod = new PProxyProcessModule();
                curProcMainMod.AutoBodyBy(ms).
                    Customize(m => m.
                        Do(curProcMainMod.FileNameGet).Expect(_ => _(curProcMainMod), Times.Once()).Returns("file path")
                    );

                var curProc = new PProxyProcess();
                curProc.StartInfoGet().BodyBy(ms).Expect(_ => _(curProc), Times.Once()).Returns(new ProcessStartInfo() { UserName = "******" });
                curProc.MainModuleGet().BodyBy(ms).Expect(_ => _(curProc), Times.Once()).Returns(curProcMainMod);
                curProc.CloseMainWindow().BodyBy(ms).Expect(_ => _(curProc), Times.Once()).Returns(true);

                PProcess.GetCurrentProcess().BodyBy(ms).Expect(_ => _(), Times.Once()).Returns(curProc);
                PProcess.StartProcessStartInfo().BodyBy(ms).Expect(_ => _(It.Is<ProcessStartInfo>(x =>
                    x.UserName == "urasandesu" &&
                    x.UseShellExecute == true &&
                    x.WorkingDirectory == "current directory" &&
                    x.FileName == "file path"
                )), Times.Once()).Returns(new PProxyProcess());

                // Act
                var result = ULProcessMixin.RestartCurrentProcess();

                // Assert
                Assert.IsTrue(result);
                ms.Verify();
            }
        }
        public void CallWebService_should_set_HttpWebRequest_to_request_textxml_content()
        {
            using (new IndirectionsContext())
            {
                // Arrange 
                var ms = new MockStorage(MockBehavior.Strict);

                // And also, you can verify whether HttpWebRequest is set intendedly if you use Moq.
                var requestProxy = new PProxyHttpWebRequest();
                requestProxy.ContentTypeSetString().BodyBy(ms).Expect(_ => _(requestProxy, "text/xml;charset=\"utf-8\""));
                requestProxy.MethodSetString().BodyBy(ms).Expect(_ => _(requestProxy, "GET"));
                requestProxy.TimeoutSetInt32().BodyBy(ms).Expect(_ => _(requestProxy, 1000));
                requestProxy.CredentialsSetICredentials().BodyBy(ms).Expect(_ => _(requestProxy, CredentialCache.DefaultNetworkCredentials));

                var responseProxy = new PProxyHttpWebResponse();
                responseProxy.StatusCodeGet().Body = @this => HttpStatusCode.OK;
                requestProxy.GetResponse().Body = @this => responseProxy;

                var url = "testService";
                PWebRequest.CreateString().Body = @this => requestProxy;


                // Act 
                var actual = new WebServiceClient().CallWebService(url);


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