public void ViewComponentInvocationTester_TestContent_should_call_validate() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _viewComponentTester.Invocation(x => x.InvokeContent).TestContent(r => { Assert.AreEqual("test content", r.Content); throw testException; })); }
public void ControllerActionTester_TestFile_should_call_validate() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _controllerTester.Action(x => x.FileAction).TestFile(r => { Assert.AreEqual("testFileName", r.FileDownloadName); throw testException; })); }
public void ValidationResultTester_WithErrorMessageStartingWith_should_throw_exception_when_expected_error_is_not_found() { AssertExceptionThrown .OfType <ValidationTestException>() .WithMessage( $"Expected error message starting with \"wrong\". Member(s): \"{nameof(StringProp1)}\"." + " Found:\nError Message: " + $"\"The {nameof(StringProp1)} field is required.\". Member(s): \"{nameof(StringProp1)}\".\n") .WhenExecuting(() => _validationForModel .When(x => x.StringProp1 = null) .ShouldReturn.ErrorFor(x => x.StringProp1) .WithErrorMessageStartingWith("wrong")); }
public void AssertAttributeValueIsWellFormedUrl_should_throw_exeption_when_value_is_not_valid_URL() { var tester = new XmlTester(FormattedXml( "<system.serviceModel><client>" + "<endpoint address=\"bad URL\" name=\"testName\" />" + "</client></system.serviceModel>")); AssertExceptionThrown .OfType <XmlTesterException>() .WithMessage("configuration/system.serviceModel/client/endpoint[@name='testName']@address:" + " Value \"bad URL\" is not a well-formed URI string.") .WhenExecuting(() => tester.AssertAttributeValueIsWellFormedUrl( ConfigXPath.ClientEndpointForName("testName"), "address")); }
public void AssertClientEndpointAddressesAreWellFormedUrls_should_throw_exeption_when_URL_is_invalid() { var tester = new XmlTester(FormattedXml( "<system.serviceModel><client>" + "<endpoint address=\"http://www.somewhere.com/something/service1.svc\" name=\"name1\" />" + "<endpoint address=\"bad url 2\" name=\"name2\" />" + "<endpoint address=\"bad url 3\" name=\"name3\" />" + "</client></system.serviceModel>")); AssertExceptionThrown .OfType <ScenarioTestFailureException>() .WithMessageContaining("configuration/system.serviceModel/client/endpoint[@name='name2']@address:" + " Value \"bad url 2\" is not a well-formed URI string.") .WhenExecuting(() => tester.AssertClientEndpointAddressesAreWellFormedUrls()); }
public void WhenExecuting_should_rethrow_caught_exception_when_not_expected_type_or_subtype() { var testException = new InvalidOperationException("fail!"); try { AssertExceptionThrown.OfTypeOrSubclassOfType <ArgumentException>().WhenExecuting(() => { throw testException; }); } catch (InvalidOperationException ex) { Assert.AreEqual(testException, ex); } }
public void WithMessageMatching_should_handle_matched_message() { ForTest.Scenarios ( new { Message = "Something bad happened.", Pattern = @"^Something bad happened\.$" }, new { Message = "Error 415.", Pattern = @"Error 4\d{2}" }, new { Message = "Invalid operation. SAD!", Pattern = @".*SAD\!" } ) .TestEach(scenario => { AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessageMatching(scenario.Pattern) .WhenExecuting(() => throw new InvalidOperationException(scenario.Message)); }); }
public void PageModelActionTester_TestPage_should_call_result_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _pageTester .Action(x => x.OnGet) .TestPage(r => { Assert.IsInstanceOfType(r, typeof(PageResult)); throw testException; })); }
public void ViewComponentInvocationTester_TestView_should_call_result_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _viewComponentTester .Invocation(x => x.Invoke) .TestView(r => { Assert.IsInstanceOfType(r, typeof(ViewViewComponentResult)); throw testException; })); }
public void PageModelActionTester_TestRedirectToRoute_should_call_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _pageTester .Action(x => x.RedirectToRouteAction) .TestRedirectToRoute("Foo/Details/3", r => { Assert.IsInstanceOfType(r, typeof(RedirectToRouteResult)); throw testException; })); }
public void AssertNoDuplicateElements_should_not_throw_exception_when_there_are_duplicates() { var tester = new XmlTester(FormattedXml(FormattedAppSettingsSection( FormattedAppSettingsElem("key1", "value1") + FormattedAppSettingsElem("key2", "value2") + FormattedAppSettingsElem("key2", "value2a") + FormattedAppSettingsElem("key3", "value3") + FormattedAppSettingsElem("key3", "value3a") + FormattedAppSettingsElem("key4", "value4") ))); AssertExceptionThrown .OfType <XmlTesterException>() .WithMessage("Multiple configuration/appSettings/add@key where key = \"key2\", \"key3\".") .WhenExecuting(() => tester.AssertNoDuplicateElements(ConfigXPath.AppSettings, "key")); }
public void ControllerActionTester_TestRedirectToPage_should_call_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _controllerTester .Action(x => x.RedirectToPageAction) .TestRedirectToPage("testPageName", r => { Assert.IsInstanceOfType(r, typeof(RedirectToPageResult)); throw testException; })); }
public void PageModelActionTester_TestRedirectToAction_should_call_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _pageTester .Action(x => x.RedirectToActionAction) .TestRedirectToAction("ActionName", "ControllerName", null, r => { Assert.IsInstanceOfType(r, typeof(RedirectToActionResult)); throw testException; })); }
public void ControllerActionTester_TestView_should_call_result_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _controllerTester .Action(x => x.DisplayTestModel) .TestView(r => { Assert.IsInstanceOfType(r, typeof(ViewResult)); throw testException; })); }
public void AssertAppSettingsValues_should_throw_exception_when_values_do_not_match() { KeyValuePair <string, string>[] appSettings = Enumerable.Range(1, 5).Select(i => new KeyValuePair <string, string>($"key{i}", $"value{i}")).ToArray(); string appSettingsXml = string.Join("\n", appSettings.Select(kv => FormattedAppSettingsElem(kv.Key, kv.Value))); appSettingsXml = appSettingsXml.Replace("value3", "value3X"); var tester = new XmlTester(FormattedXml(FormattedAppSettingsSection(appSettingsXml))); AssertExceptionThrown .OfType <ScenarioTestFailureException>() .WithMessageContaining("Expected: <value3> actual: <value3X>") .WhenExecuting(() => tester.AssertAppSettingsValues(appSettings)); }
public void PageModelActionTester_TestPage_should_call_model_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _pageTester .Action(x => x.OnGet) .ExpectingModel(m => { throw testException; }) .TestPage()); }
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()); }
public void ControllerActionTester_TestPartialView_should_call_model_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _controllerTester .Action(x => x.PartialViewAction) .ExpectingModel <TestModel>(m => { Assert.IsInstanceOfType(m, typeof(TestModel)); throw testException; }) .TestPartialView()); }
public void ViewComponentInvocationTester_TestView_should_call_model_validate_method() { var testException = new InvalidOperationException("test exception"); AssertExceptionThrown .OfType <InvalidOperationException>() .WithMessage(testException.Message) .WhenExecuting(() => _viewComponentTester .Invocation(x => x.Invoke) .ExpectingModel <TestModel>(m => { Assert.IsInstanceOfType(m, typeof(TestModel)); throw testException; }) .TestView()); }
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 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 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 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 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" } })); }
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()); }); }
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)); }
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)); }
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 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>()); }