public MainForm()
        {
            InitializeComponent();

            // populate list of Employee and Manager
            _peopleList = Mocked.PeopleList();
        }
        public void HandleRequest_SendsRequestToFirstMatchingRoute()
        {
            var routes = new List <IRoute>
            {
                MockRepository.GenerateStub <IRoute>(),
                MockRepository.GenerateStub <IRoute>(),
                MockRepository.GenerateStub <IRoute>()
            };

            routes[0].Stub(x => x.IsMatch("/test.html")).Return(false);
            routes[1].Stub(x => x.IsMatch("/test.html")).Return(true);
            routes[2].Stub(x => x.IsMatch("/test.html")).Return(true);

            var context = Mocked.HttpContext()
                          .WithUrl("http://localhost/test.html")
                          .Mock;

            context.Stub(x => x.ServerInfo).Return(MockRepository.GenerateStub <IServerInfo>());
            context.ServerInfo.Stub(x => x.Routes).Return(routes);

            var handler = new RouteRequestHandler();

            handler.HandleRequest(context);

            routes[1].AssertWasCalled(x => x.HandleRequest(context));
        }
Example #3
0
        static void Main(string[] args)
        {
            //System.Diagnostics.Debugger.Launch();
            //Console.WriteLine();
            //OnMethodToInstrument("hello", new EventArgs());
            //Console.WriteLine();

            // Without mocking enabled (the default)
            Console.WriteLine(new string('#', 90));
            Console.WriteLine("Calling ClassToMock.StaticMethodToMock() (a static method in a sealed class)");
            var result = ClassToMock.StaticMethodToMock();

            Console.WriteLine("Result: " + result);
            Console.WriteLine(new string('#', 90) + "\n");

            // With mocking enabled, doesn't call into the static method, calls the mocked version instead
            Console.WriteLine(new string('#', 90));
            Mocked.SetReturnValue = 1;
            Console.WriteLine("Turning ON mocking of \"Profilier.ClassToMock.StaticMethodToMock\"");
            //Mocked.Configure(typeof(ClassToMock).FullName + ".StaticMethodToMock", mockMethod: true);
            Mocked.Configure("ProfilerTarget.ClassToMock.StaticMethodToMock", mockMethod: true);

            Console.WriteLine("Calling ClassToMock.StaticMethodToMock() (a static method in a sealed class)");
            result = ClassToMock.StaticMethodToMock();
            Console.WriteLine("Result: " + result);
            Console.WriteLine(new string('#', 90) + "\n");

            Console.WriteLine(new string('=', 20) + "测试注入--开始" + new string('=', 20));

            InjectTest.SayHello("waodng", new EventArgs());

            Console.WriteLine(new string('=', 20) + "测试注入--结束" + new string('=', 20));
            Console.Read();
        }
        public async Task Returns_Ok_Given_ExistingCode()
        {
            // Arrange
            const string code    = "Existing Code";
            var          country = Mocked.Country(code);

            _countryRepositoryMock
            .Setup(x => x.SearchByCodeAsync(code))
            .Returns(Task.FromResult <Country>(country));

            // Act
            var response = await Client.GetAsync($"/country/{code}");

            // Assert
            response.StatusCode.Should().Be(StatusCodes.Status200OK);

            var responseString = await response.Content.ReadAsStringAsync();

            var responseObject = JsonConvert.DeserializeObject <VmCountryDetails>(responseString);

            responseObject.Flag.Should().Be(country.Flag);
            responseObject.Region.Should().Be(country.Region);
            responseObject.Name.Should().Be(country.Name);
            responseObject.Population.Should().Be(country.Population);
            responseObject.Alpha3Code.Should().Be(country.Alpha3Code);
            responseObject.CapitalCity.Should().Be(country.Capital);
            responseObject.Languages.Should().BeEquivalentTo(country.Languages);
            responseObject.Currencies.Should().BeEquivalentTo(country.Currencies);
            responseObject.Timezones.Should().BeEquivalentTo(country.Timezones);
            responseObject.BorderingCountries.Should().BeEquivalentTo(country.Borders);
        }
Example #5
0
        static void Main(string[] args)
        {
            WriteHeader("with expressions");

            Person?person = Mocked.People().FirstOrDefault(peep => peep.FirstName == "Karen");


            WriteSectionBold("Founded", false);

            WriteIndented(person?.ToString());

            EmptyLine();


            if (person is not null)
            {
                var otherPerson = person with {
                    LastName = "Black"
                };

                WriteSectionBold("using with", false);

                WriteIndented(otherPerson.ToString());
            }

            Console.ReadLine();
        }
    }
Example #6
0
        public async Task Returns_OkResult()
        {
            // Arrange
            const string code = "Country code";

            var country = Mocked.Country(code);

            GetMock <ICountryDetailsService>()
            .Setup(x => x.Get(code))
            .Returns(Task.FromResult(country));

            // Act
            var result = await ClassUnderTest.Details(code);

            // Assert
            var okResult    = Assert.IsType <OkObjectResult>(result.Result);
            var returnValue = Assert.IsType <VmCountryDetails>(okResult.Value);

            returnValue.Flag.Should().Be(country.Flag);
            returnValue.Region.Should().Be(country.Region);
            returnValue.Name.Should().Be(country.Name);
            returnValue.Population.Should().Be(country.Population);
            returnValue.Alpha3Code.Should().Be(country.Alpha3Code);
            returnValue.CapitalCity.Should().Be(country.Capital);
            returnValue.Languages.Should().BeEquivalentTo(country.Languages);
            returnValue.Currencies.Should().BeEquivalentTo(country.Currencies);
            returnValue.Timezones.Should().BeEquivalentTo(country.Timezones);
            returnValue.BorderingCountries.Should().BeEquivalentTo(country.Borders);
        }
        public override Mock <HttpContextBase> CreateDefault()
        {
            var itemDictionary = new Dictionary <string, object>();

            Mocked.SetupGet(p => p.Items).Returns(itemDictionary);

            return(Mocked);
        }
Example #8
0
        /// <summary>
        /// An example of how the instrumentation would look - use this under ILSpy, Reflector
        /// or ILDASM to see the actual IL. We are going to use this as a template
        /// </summary>
        public static int StaticMethodToMockWhatWeWantToDo()
        {
            // Inject the IL to do this instead!!
            if (Mocked.ShouldMock("Profilier.ClassToMock.StaticMethodToMockWhatWeWantToDo"))
            {
                return(Mocked.MockedMethod());
            }

            Log("StaticMethodToMockWhatWeWantToDo called, returning 42");
            return(42);
        }
        public void HandleRequest_ForUnsupportedRequest_DoesNotCallTheAction()
        {
            var route  = new Route("tests/([a-z]+).html", typeof(TestAction));
            var action = new TestAction();

            var httpContext = Mocked.HttpContext()
                              .WithRegisteredAction(action)
                              .WithHttpMethod("PUT")
                              .WithUrl("http://localhost/tests/request.html")
                              .Mock;

            route.HandleRequest(httpContext);

            Assert.Null(action.LastMethod);
        }
Example #10
0
        public void HandleRequest_ForPostRequest_CallsThePostMethodOnTheAction()
        {
            var route  = new Route("tests/([a-z]+).html", typeof(TestAction));
            var action = new TestAction();

            var httpContext = Mocked.HttpContext()
                              .WithRegisteredAction(action)
                              .WithHttpMethod("POST")
                              .WithUrl("http://localhost/tests/request.html")
                              .Mock;

            route.HandleRequest(httpContext);

            Assert.Equal("Post", action.LastMethod);
        }
Example #11
0
        public void HandleRequest_ForGetRequest_PassesCorrectDataToAction()
        {
            var route  = new Route("tests/(?<pageName>[a-z]+).html", typeof(TestAction));
            var action = new TestAction();

            var httpContext = Mocked.HttpContext()
                              .WithRegisteredAction(action)
                              .WithHttpMethod("GET")
                              .WithUrl("http://localhost/tests/myRequest.html")
                              .Mock;

            route.HandleRequest(httpContext);

            Assert.Equal("myRequest", action.LastContext.RouteData["pageName"]);
            Assert.Same(httpContext, action.LastContext.HttpContext);
        }
        public void HandleRequest_WithNoMatchingRoute_ReturnsFalse()
        {
            var routes = new List <IRoute> {
                MockRepository.GenerateStub <IRoute>()
            };

            var context = Mocked.HttpContext()
                          .WithUrl("http://localhost/test.html")
                          .Mock;

            context.Stub(x => x.ServerInfo).Return(MockRepository.GenerateStub <IServerInfo>());
            context.ServerInfo.Stub(x => x.Routes).Return(routes);

            var handler = new RouteRequestHandler();
            var result  = handler.HandleRequest(context);

            Assert.False(result);
        }
Example #13
0
        public void HandleRequest_WhenRequestWasHandled_ExecutesTheResult()
        {
            var route  = new Route("tests/([a-z]+).html", typeof(TestAction));
            var action = new TestAction
            {
                GetResult = MockRepository.GenerateStub <IActionResult>()
            };

            var httpContext = Mocked.HttpContext()
                              .WithRegisteredAction(action)
                              .WithHttpMethod("GET")
                              .WithUrl("http://localhost/tests/request.html")
                              .Mock;

            route.HandleRequest(httpContext);

            action.GetResult.AssertWasCalled(x => x.Execute(Arg <IHttpContext> .Is.Anything));
        }
Example #14
0
        public void HandleRequest_WhenRequestWasNotHandled_ReturnsFalse()
        {
            var route  = new Route("tests/([a-z]+).html", typeof(TestAction));
            var action = new TestAction
            {
                GetResult = null
            };

            var httpContext = Mocked.HttpContext()
                              .WithRegisteredAction(action)
                              .WithHttpMethod("GET")
                              .WithUrl("http://localhost/tests/request.html")
                              .Mock;

            var result = route.HandleRequest(httpContext);

            Assert.False(result);
        }
Example #15
0
        public void HandleRequest_WhenRequestWasHandled_ReturnsTrue()
        {
            var route  = new Route("tests/([a-z]+).html", typeof(TestAction));
            var action = new TestAction
            {
                GetResult = MockRepository.GenerateStub <IActionResult>()
            };

            var httpContext = Mocked.HttpContext()
                              .WithRegisteredAction(action)
                              .WithHttpMethod("GET")
                              .WithUrl("http://localhost/tests/request.html")
                              .Mock;

            var result = route.HandleRequest(httpContext);

            Assert.True(result);
        }
Example #16
0
        private static void Main(string[] args)
        {
            /*
             * new expression or target typing
             */
            List <Person> people = new();

            foreach (var person in Mocked.PeopleList())
            {
                people.Add(person);
            }

            Console.WriteLine("People:");

            foreach (var person in people)
            {
                ConsoleWriter.WriteConsoleColorNewLine(
                    new ColoredString(ConsoleColor.White, $"{person.Id} "),
                    new ColoredString(ConsoleColor.Yellow, $"{person.FullName}"),
                    new ColoredString(ConsoleColor.Red, $"{(person is Manager ? " manager" : " employee")}"));
            }

            Console.ReadLine();
        }