title: Unit Test for Writing to a Text File
body: <p>I am trying to write a unit test to make sure that the method is writing to a text file. My method writes to the text file <code>MovieList.txt</code>. I kept getting an error saying that it cannot access the file because it is being used by another process. So I tried changing the method to accept a parameter to write to a different text file, <code>MovieListTEST.txt</code>, but I still can't figure it out. Anyone know how to properly do this?</p>
  
  <p>This is my code to write to the file:</p>
  
  <pre><code>public bool WriteMovieListToFile()
  {
      try
      {
          FileStream fs = new FileStream("MovieList.txt", FileMode.Append, FileAccess.Write);
          StreamWriter textWriter = new StreamWriter(fs);
  
          textWriter.WriteLine(movie.Title);
          textWriter.WriteLine(movie.Genre);
          textWriter.WriteLine(movie.Actor);
          textWriter.WriteLine(movie.Year);
  
          textWriter.Close();
          return true;
      }
      catch (Exception ex)
      {
          Console.WriteLine("Error:" + ex.Message);
          return false;
      }
  }
  </code></pre>
  
  <p>Edited with param:</p>
  
  <pre><code>public bool WriteMovieListToFile(string fileLocation)
  {
      try
      {
          FileStream fs = new FileStream(fileLocation, FileMode.Append, FileAccess.Write);
          StreamWriter textWriter = new StreamWriter(fs);
  </code></pre>
  
  <p>This is my unit test code:</p>
  
  <pre><code>/// &lt;summary&gt;
  ///A test for WriteMovieListToFile
  ///&lt;/summary&gt;
  [TestMethod()]
  public void WriteMovieListToFileTest1()
  {
      Movie movie1 = new Movie("Title", "Genre", "Actor", "Year");
      movieSystem.AddMovie(movie1);
      movieSystem.WriteMovieListToFile("MovieListTEST.txt");
  
      var fileText = File.ReadLines("MovieListTEST.txt");
      Assert.IsTrue(fileText.ToString().Length &gt; 1);
  }
  </code></pre>