title: How to test for exceptions thrown using xUnit, SubSpec and FakeItEasy
body: <p>I’m using xUnit, SubSpec and FakeItEasy for my unit tests.
  I’ve so far created some positive unit tests like the following:</p>
  
  <pre><code>"Given a Options presenter"
      .Context(() =&gt;
          presenter = new OptionsPresenter(view,
                                           A&lt;IOptionsModel&gt;.Ignored,
                                           service));
  
  "with the Initialize method called to retrieve the option values"
      .Do(() =&gt; 
          presenter.Initialize());
  
  "expect the view not to be null"
      .Observation(() =&gt;
          Assert.NotNull(view));
  
  "expect the view AutoSave property to be true"
      .Observation(() =&gt; Assert.True(view.AutoSave));
  </code></pre>
  
  <p>But now I want to write some negative unit tests and check that certain methods don't get  called, and an exception is thrown</p>
  
  <p>e.g.</p>
  
  <pre><code>"Given a Options presenter"
      .Context(() =&gt;
          presenter = new OptionsPresenter(view,
                                           A&lt;IOptionsModel&gt;.Ignored,
                                           service));
  
  "with the Save method called to save the option values"
      .Do(() =&gt; 
          presenter.Save());
  
  "expect an ValidationException to be thrown"
      .Observation(() =&gt;
          // TODO 
       );
  
  "expect an service.SaveOptions method not to be called"
      .Observation(() =&gt;
          // TODO 
       );
  </code></pre>
  
  <p>I can see FakeItEasy has a MustNotHaveHappened extension method, and xUnit has an Assert.Throws method.</p>
  
  <p>But how do I put it all together? </p>
  
  <p>The exception I want to test for should occur when the Save method is called. So I’m guessing I should wrap an Assert.Throws method around the presenter.Save() method call, but I thought the presenter.Save method should be called in the .Do(() => ...</p>
  
  <p>Can you please advise if my unit test should look like below or something else?</p>
  
  <pre><code>"Given a Options presenter"
      .Context(() =&gt;    
          presenter = new OptionsPresenter(view,
                                           model,
                                           service));
  
  "expect the Presenter.Save call to throw an Exception"
      .Observation(() =&gt;
          Assert.Throws&lt;FluentValidation.ValidationException&gt;(() =&gt; presenter.Save()));
  
  "expect the Service.SaveOptions method not to be called"
      .Observation(() =&gt;
          A.CallTo(() =&gt; service.SaveOptions(A&lt;IOptionsModel&gt;.Ignored)).MustNotHaveHappened());
  </code></pre>
  
  <p>Many thanks</p>