public void ControllerActionTester_TestPartialView_should_call_result_validate_method()
        {
            var testException = new InvalidOperationException("test exception");

            AssertExceptionThrown
            .OfType <InvalidOperationException>()
            .WithMessage(testException.Message)
            .WhenExecuting(() =>
                           _controllerTester
                           .Action(x => x.PartialViewAction)
                           .TestPartialView(r =>
            {
                Assert.IsInstanceOfType(r, typeof(PartialViewResult));
                throw testException;
            }));
        }
        public void ControllerActionTester_TestRedirectToAction_should_call_validate_method()
        {
            var testException = new InvalidOperationException("test exception");

            AssertExceptionThrown
            .OfType <InvalidOperationException>()
            .WithMessage(testException.Message)
            .WhenExecuting(() =>
                           _controllerTester
                           .Action(x => x.RedirectToActionAction)
                           .TestRedirectToAction("ActionName", r =>
            {
                Assert.IsInstanceOfType(r, typeof(RedirectToRouteResult));
                throw testException;
            }));
        }
示例#3
0
        public void ControllerActionTester_TestView_should_call_model_validate_method()
        {
            var testException = new InvalidOperationException("test exception");

            AssertExceptionThrown
            .OfType <InvalidOperationException>()
            .WithMessage(testException.Message)
            .WhenExecuting(() =>
                           _controllerTester
                           .Action(x => x.DisplayTestModel)
                           .ExpectingModel <TestModel>(m =>
            {
                Assert.IsInstanceOfType(m, typeof(TestModel));
                throw testException;
            })
                           .TestView());
        }
示例#4
0
        public void PageModelActionTester_TestJsonResult_should_call_model_validate_method()
        {
            var testException = new InvalidOperationException("test exception");

            AssertExceptionThrown
            .OfType <InvalidOperationException>()
            .WithMessage(testException.Message)
            .WhenExecuting(() =>
                           _pageTester
                           .Action(x => x.Json)
                           .ExpectingModel <TestModel>(m =>
            {
                Assert.IsInstanceOfType(m, typeof(TestModel));
                throw testException;
            })
                           .TestJsonResult());
        }
示例#5
0
        public void Original_AppSettings_values_should_be_restored_after_Test_exception()
        {
            string expected = GetAppSettingsString();

            AssertExceptionThrown.OfType <Exception>().WithMessage("boom").WhenExecuting(() =>
                                                                                         AppSettingsHelper
                                                                                         .WithAppSetting("test1", "changed value 1")
                                                                                         .AndAppSetting("testx", "added value")
                                                                                         .Test(() =>
            {
                throw new Exception("boom");
            })
                                                                                         );

            string actual = GetAppSettingsString();

            Assert.AreEqual(expected, GetAppSettingsString());
        }
        public void Enumerable_TestEach_extension_method_should_throw_failure_exception_when_not_all_exceptions_are_inconclusive()
        {
            var scenarios = new[] { "a", "b", "c" };

            AssertExceptionThrown
            .OfType <SparkyTestHelpers.Scenarios.ScenarioTestFailureException>()
            .WhenExecuting(() => scenarios.TestEach(scenario =>
            {
                switch (scenario)
                {
                case "b":
                    throw new AssertInconclusiveException("inconclusive");

                default:
                    throw new InvalidOperationException("fail");
                }
            }));
        }
        public void TestEach_should_throw_failure_exception_when_not_all_exceptions_are_inconclusive()
        {
            var scenarios = new[] { "a", "b", "c" };

            AssertExceptionThrown
            .OfType <ScenarioTestFailureException>()
            .WhenExecuting(() => new MsTestScenarioTester <string>(scenarios)
                           .TestEach(scenario =>
            {
                switch (scenario)
                {
                case "b":
                    throw new AssertInconclusiveException("inconclusive");

                default:
                    throw new InvalidOperationException("fail");
                }
            }));
        }
        public void WithMessage_should_handle_unmatched_message()
        {
            const string message  = "Crap";
            string       expected =
                $"Expected message \"Whoops\". Actual: \"{message}\"."
                + "\n(message from System.InvalidOperationException.)";

            try
            {
                AssertExceptionThrown
                .OfType <InvalidOperationException>()
                .WithMessage("Whoops")
                .WhenExecuting(() => throw new InvalidOperationException(message));
            }
            catch (ExpectedExceptionNotThrownException ex)
            {
                Assert.AreEqual(expected, ex.Message);
            }
        }
        public void WithMessageContaining_should_handle_unmatched_message()
        {
            const string message = "This is not the message you're looking for.";

            string expected =
                $"Expected message containing \"Whoops!\". Actual: \"{message}\"."
                + "\n(message from System.InvalidOperationException.)";

            try
            {
                AssertExceptionThrown
                .OfType <InvalidOperationException>()
                .WithMessageContaining("Whoops!")
                .WhenExecuting(() => throw new InvalidOperationException(message));
            }
            catch (ExpectedExceptionNotThrownException ex)
            {
                Assert.AreEqual(expected, ex.Message);
            }
        }
        public void AssertAttributeValues_should_throw_exception_when_values_do_not_match()
        {
            string appSettingsXml = FormattedAppSettingsSection(
                "<add key=\"testKey\" value=\"testValue\" value2=\"testValue2X\" value3=\"testValue3\" />");

            var tester = new XmlTester(FormattedXml(appSettingsXml));

            AssertExceptionThrown
            .OfType <ScenarioTestFailureException>()
            .WithMessageContaining("@value2: Expected: <testValue2> actual: <testValue2X>")
            .WhenExecuting(() =>
                           tester.AssertAttributeValues(
                               ConfigXPath.AppSettingForKey("testKey"),
                               new Dictionary <string, string>
            {
                { "value", "testValue" },
                { "value2", "testValue2" },
                { "value3", "testValue3" }
            }));
        }
示例#11
0
        public void Where_extension_with_action_should_work()
        {
            _test.WithInt(7);

            AssertExceptionNotThrown.WhenExecuting(() =>
                                                   _mock.Verify(x => x.WithInt(It.Is <int>(i => i > 6)), Times.Once));

            AssertExceptionNotThrown.WhenExecuting(() =>
                                                   _mock.Verify(x => x.WithInt(Any.Int.Where(i => Assert.IsTrue(i > 6))), Times.Once));

            AssertExceptionThrown
            .OfType <AssertFailedException>()
            .WithMessageContaining("Assert.AreEqual failed. Expected:<3>. Actual:<7>")
            .WhenExecuting(() => _mock.Verify(x => x.WithInt(Any.Int.Where(i => Assert.AreEqual(3, i))), Times.Once));

            AssertExceptionThrown
            .OfType <MockException>()
            .WithMessageContaining("once, but was 0 times")
            .WhenExecuting(() => _mock.Verify(x => x.WithString(Any.String.Where(s => s.Contains("x"))), Times.Once));
        }
示例#12
0
 public void PageModelActionTester_Test_methods_should_throw_exception_when_result_is_not_expected_IActionResult_type()
 {
     ForTest.Scenarios
     (
         new ActionTypeScenario(() => _pageTester.Action(x => x.OnGet).TestContent(), typeof(ContentResult), typeof(PageResult)),
         new ActionTypeScenario(() => _pageTester.Action(x => x.OnGet).TestFile(), typeof(FileResult), typeof(PageResult)),
         new ActionTypeScenario(() => _pageTester.Action(x => x.OnGet).TestJsonResult(), typeof(JsonResult), typeof(PageResult)),
         new ActionTypeScenario(() => _pageTester.Action(x => x.OnGet).TestRedirectToAction("action", "controller"), typeof(RedirectToActionResult), typeof(PageResult)),
         new ActionTypeScenario(() => _pageTester.Action(x => x.OnGet).TestRedirectToPage("testPageName"), typeof(RedirectToPageResult), typeof(PageResult)),
         new ActionTypeScenario(() => _pageTester.Action(x => x.OnGet).TestRedirectToRoute("x"), typeof(RedirectToRouteResult), typeof(PageResult)),
         new ActionTypeScenario(() => _pageTester.Action(x => x.OnGet).TestResult <JsonResult>(), typeof(JsonResult), typeof(PageResult))
     )
     .TestEach(scenario =>
     {
         AssertExceptionThrown
         .OfType <ActionTestException>()
         .WithMessage($"Expected IActionResult type {scenario.ExpectedTypeName}. Actual: {scenario.ActualTypeName}.")
         .WhenExecuting(() => scenario.TestAction());
     });
 }
示例#13
0
        public void ShouldBeStringMatchFor_should_work_as_expected()
        {
            var restaurant = new Restaurant {
                Id = 24, Name = "TestName", Cuisine = CuisineType.German
            };
            var diner = new Diner {
                Id = 24, Name = "TestName", Cuisine = FoodType.German
            };

            var mapTester = MapTester.ForMap <Restaurant, Diner>()
                            .WhereMember(dest => dest.Cuisine).ShouldBeStringMatchFor(src => src.Cuisine);

            AssertExceptionNotThrown.WhenExecuting(() => mapTester.AssertMappedValues(restaurant, diner));

            diner.Cuisine = FoodType.Italian;

            AssertExceptionThrown
            .OfType <MapTesterException>()
            .WithMessageContaining("Mapping test failed for property \"Cuisine\". Expected<German>. Actual: <Italian>")
            .WhenExecuting(() => mapTester.AssertMappedValues(restaurant, diner));
        }
示例#14
0
 public void ControllerActionTester_Test_methods_should_throw_exception_when_result_is_not_expected_ActionResult_type()
 {
     ForTest.Scenarios
     (
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestContent(), typeof(ContentResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestEmpty(), typeof(EmptyResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestFile(), typeof(FileResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestJson(), typeof(JsonResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestPartialView(), typeof(PartialViewResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestRedirectToAction("x"), typeof(RedirectToRouteResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestRedirect("testUrl"), typeof(RedirectResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestRedirectToRoute("x"), typeof(RedirectToRouteResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Index).TestResult <JsonResult>(), typeof(JsonResult), typeof(ViewResult)),
         new ActionTypeScenario(() => _controllerTester.Action(x => x.Json).TestView(), typeof(ViewResult), typeof(JsonResult))
     )
     .TestEach(scenario =>
     {
         AssertExceptionThrown
         .OfType <ControllerTestException>()
         .WithMessage($"Expected ActionResult type {scenario.ExpectedTypeName}. Actual: {scenario.ActualTypeName}.")
         .WhenExecuting(() => scenario.TestAction());
     });
 }
        public void OfType_factory_method_should_return_AssertExceptionThrown_instance()
        {
            AssertExceptionThrown instance = AssertExceptionThrown.OfType <Exception>();

            Assert.IsNotNull(instance);
        }
 private static void WithExceptionTest(Action assertConfigurationIsValid)
 {
     AssertExceptionThrown.OfType <AutoMapperConfigurationException>()
     .WithMessageContaining("If you want to ignore")
     .WhenExecuting(assertConfigurationIsValid);
 }
 public void Enumerable_TestEach_extenion_method_should_throw_ScenarioTestFailureException_with_proper_message_for_tuple_scenario()
 {
     AssertExceptionThrown
     .OfType <ScenarioTestFailureException>()
     .WithMessageContaining("Scenario data - System.ValueTuple[[System.Int32],[System.String]]: {\"Item1\":3,\"Item2\":\"a\"}")
     .WhenExecuting(() => new[] { (Input: 3, Expected: "a") }.TestEach(_ => Assert.Fail()));
示例#18
0
 public void MsTest_ForTest_EnumValues_should_throw_InvalidOperationException_when_type_is_not_enum_type()
 {
     AssertExceptionThrown.OfType <InvalidOperationException>()
     .WithMessage("System.DateTime is not an Enum type.")
     .WhenExecuting(() => ForTest.EnumValues <DateTime>());
 }
示例#19
0
 public void WhereMember_should_throw_expected_exception_for_bad_property_name_expression()
 {
     AssertExceptionThrown
     .OfType <MapTesterException>().WithMessage("Invalid property expression: \"dest => dest.Name.ToString()\".")
     .WhenExecuting(() => _mapTester.WhereMember(dest => dest.Name.ToString()));
 }