C# Unit Test with External File Dependancies

C#
Copy to clipboard
fablecode
  1. // Source: http://www.hanselman.com/blog/UsingNUnitAndABetterWayToUnitTestWithExternalFileDependancies.aspx
  2.  
  3. [SetUp]
  4. public void SetUp()
  5. {
  6.         Assembly a = Assembly.GetExecutingAssembly();
  7.         using (Stream s = a.GetManifestResourceStream("MyNameSpace.something.txt"))
  8.         {
  9.                 using (StreamReader sr = new StreamReader(s))
  10.                 {
  11.                         using (StreamWriter sw = File.CreateText(webConfigPath))
  12.                         {
  13.                                 sw.Write(sr.ReadToEnd());
  14.                                 sw.Flush();
  15.                         }
  16.                 }
  17.         }
  18. }
  19.  
  20. [TearDown]
  21. public void TearDown()
  22. {
  23.         if(File.Exists(webConfigPath))
  24.         {
  25.                 File.Delete(webConfigPath);
  26.         }
  27. }

    Leave a comment

     
    Visual verification
     

    Related books

    More InfoLearn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    Learn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    More InfoC# 6.0 and the .NET 4.6 Framework
    C# 6.0 and the .NET 4.6 Framework
    More InfoMicrosoft Visual C# Step by Step (8th Edition) (Developer Reference)
    Microsoft Visual C# Step by Step (8th Edition) (Developer Reference)
    More InfoThe C# Player's Guide (3rd Edition)
    The C# Player's Guide (3rd Edition)
    More InfoThe C# Player's Guide (2nd Edition)
    The C# Player's Guide (2nd Edition)
    More InfoC# 6.0 in a Nutshell: The Definitive Reference
    C# 6.0 in a Nutshell: The Definitive Reference
    More InfoC# Programming: The No-Nonsense Guide: Learn C# Programming Within 12 Hours! (Including A Free C# Cheatsheet & 30+ Exercises)
    C# Programming: The No-Nonsense Guide: Learn C# Programming Within 12 Hours! (Including A Free C# Cheatsheet & 30+ Exercises)
    More InfoC#: C# Crash Course - The Complete Beginner's Course to Learn C# Programming in 15 Clear-Cut Lessons - Including Dozens of Practical Examples & Exercises (C# Series)
    C#: C# Crash Course - The Complete Beginner's Course to Learn C# Programming in 15 Clear-Cut Lessons - Including Dozens of Practical Examples & Exercises (C# Series)