Created
January 16, 2013 16:54
-
-
Save anonymous/4548768 to your computer and use it in GitHub Desktop.
For unit tests that need to load a file. Locating the file is always a problem, instead make it a resource and then call SerializedResource() to write the files to disk. To locate the file just call GetFullname("file1.txt") to get the true name. #nunit #mstest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private string BasePath | |
| { | |
| get | |
| { | |
| return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
| } | |
| } | |
| private void SerializeResources() | |
| { | |
| foreach (var s in Assembly.GetExecutingAssembly().GetManifestResourceNames()) | |
| { | |
| string filename = BasePath + "\\" + s; | |
| using (StreamWriter writer = new StreamWriter(filename)) | |
| using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(s))) | |
| { | |
| writer.Write(reader.ReadToEnd()); | |
| } | |
| } | |
| } | |
| private string GetFullname(string filename) | |
| { | |
| return BasePath + "\\" + GetType().Namespace + "." + filename; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add a default constructor to the unit test class. Inside the constructor, call SerializedResources() to write the resources to disk. This will work in NUnit and MsTest