title: How do unit test for a received call within a method that throws an exception
body: <p>I am trying to unit test a method that throws an exception, and before throwing it has to perform a few tasks, like logging. I am using <code>NSubstitute</code> and can't get my head around this one.</p>
  
  <p>so my test looks like this </p>
  
  <pre><code>[TestMethod]
  [ExpectedException(typeof(IOException))]
  public void RecordAnalyser_FileReadFailedInFirstAttempt_WarningLogged()
  {
      //Arrange
      var fileMock = Substitute.For&lt;IFile&gt;();
      fileMock.ReadLines(Arg.Any&lt;string&gt;()).Throws(new IOException());
  
      //Act
      var recordAnalyser = new RecordAnalyser(fileMock, logger); //--&gt; throws exception.
  
      //Assert
      logger.Received(1).Warn(Arg.Any&lt;string&gt;(), Arg.Any&lt;Exception&gt;());
  }
  </code></pre>
  
  <p>Now i want to  assert if logger received a warning log, but since the line above sends an exception, and i have an expected exception attribute, test doesn't come to check for assertion.</p>
  
  <p>One dirty code i can think of is to wrap the error statement in a try catch within the test, but its not the neatest.</p>
  
  <pre><code>//Act
  try
  {
      var recordAnalyser = new RecordAnalyser(fileMock, logger);
  }
  catch (Exception)
  {
      // eat
  }
  </code></pre>
  
  <p>Code under test -</p>
  
  <pre><code>public RecordAnalyser(IFile file, ILoggerService logger)
  {
      this.logger = logger;
      try
      {
          names = file.ReadLines(Constants.Names).ToList();
      }
      catch (System.IO.IOException e)
      {
          logger.Error("Names file could not be read.", ex);
        // How do I test above line without a try catch block in unit test 
          throw;
      } 
  }
  </code></pre>
  
  <p>looking for suggestions here.</p>