/// <summary> /// Create a new scenario with the given title, ensuring the previous scenario was properly /// cleaned up. Suggested if you use MBUnit or NUnit to /// create a base class with a method 'Scenario()' which uses the current test's naame. E.g. /// /// protected Scenario(){ /// return Scenario(TestContext.CurrentContext.Test.Name); /// } /// </summary> /// <param name="title">the scenario title. Can not be empty or null</param> /// <returns>a newly created scenario</returns> public Scenario Scenario(String title) { //ensure previous scenario has completed to prevent dangling scenarios //i.e. those which look like they have passed but haven't actually run if (CurrentScenario != null) { try { AfterTest(); } catch (AssertionFailedException e) { throw new AssertionFailedException("Previous Scenario failed", e); } BeforeTest(); } if (String.IsNullOrWhiteSpace(title)) { TestFirstAssert.Fail("Scenario's require a title"); } //Console.WriteLine("New Scenario:" + title); OnBeforeNewScenario(); CurrentScenario = new Scenario(title, Injector); return(CurrentScenario); }
/// <summary> /// Mark the scenario as failed if the match failed. If it passed _dont_ mark scenario as having been run /// as this could be called from any step( given, check, when...) /// </summary> private void FailIfNotPassed <T>(Boolean passed, T actual, IMatcher <T> matcher, IMatchDiagnostics diagnostics) { MarkLastStepPassed(passed); if (!passed) { Passed(false); var desc = new Description(); desc.Child("Steps were", StepsToString()); desc.Child("expected", matcher); if (actual is String) { desc.Child("but was", "'" + actual + "'"); } else { desc.Child("but was", actual); } desc.Text("==== Diagnostics ===="); desc.Child(diagnostics); TestFirstAssert.Fail(Environment.NewLine + desc.ToString()); } }
/// <summary> /// Throw exception if not currently in a scenario /// </summary> public void AssertInScenario() { if (CurrentScenario == null) { TestFirstAssert.Fail("No scenario. You can only invoke this operation within the scope of a currently in progress scenario"); } }
private void GenerateAndThrowFailMsg(Object actual, IMatcher matcher, MatchDiagnostics diag, Object label) { var desc = new Description(); if (label != null) { desc.Child("for", label); } Expect.PrintExpectButGot(desc, actual, matcher); desc.Text("==== Diagnostics ===="); desc.Child(diag); TestFirstAssert.Fail(Environment.NewLine + desc.ToString()); }
/// <summary> /// Check that this scenario has had at least one 'Then' step invoked, and that all 'Thens' have been successful. /// /// Throw an AssertionFailedException if these conditions do not hold true. /// /// If this method does not throw an exception consider the scenario as passing /// </summary> public void AssertHasRunAndPassed() { RunOnEndListeners(); if (m_state == State.NotRun) { TestFirstAssert.Fail("Scenario '{0}' has not run. Did you invoke Then(..)? (if previous errors this may have caused the no run). \n\n{1}", Title, NewFailedMsg()); } if (m_state != State.Passed) { if (m_failingException != null) { TestFirstAssert.Fail(m_failingException, "Scenario '{0}' failed with exception", Title); } else { TestFirstAssert.Fail("Scenario '{0}' failed", Title); } } }