title: xUnit Making tests for reading and writing to Json file
body: <p>I'm creating a new application in NancyFx and I'm trying to do Test Driven Development (TDD) So the first features I want to develop are reading and writing to a json file. Now I know this is quite hard to do because it involves <code>httpContext</code> which I obviously can't do unless the application is running. </p>
  
  <p>What I would like to do is mock this so I can create a unit test for reading and writing to a json file. Could anyone show me an example of how I might do this and explain how you have done it?</p>
  
  <p>I have two methods one for reading and one for writing shown below:</p>
  
  <p>ReadToJsonFile Method:</p>
  
  <pre><code>public IEnumerable&lt;T&gt; ReadFile&lt;T&gt;(string fileName)
      {
          try
          {
              var readFile = File.ReadAllText(HttpContext.Current.Server.MapPath(fileName));
              var list = JsonConvert.DeserializeObject&lt;List&lt;T&gt;&gt;(readFile);
              return list;
          }
          catch (Exception ex)
          {
              _logger.LogException(ex);
              return null;
          }
      }
  </code></pre>
  
  <p>WriteToJsonFile Method:</p>
  
  <pre><code>public bool WriteFile&lt;T&gt;(string fileName, IEnumerable&lt;T&gt; list)
      {
          try
          {
              var listContent = JsonConvert.SerializeObject(list, Formatting.Indented);
              File.WriteAllText(HttpContext.Current.Server.MapPath(fileName), listContent);
              return true;
          }
          catch (Exception ex)
          {
              _logger.LogException(ex);
              return false;
          }
      }
  </code></pre>
  
  <p>Any suggestions would be brilliant, Thanks.</p>