I am having an issue with NCrunch where I am getting the following exception when I test a method that is trying to access the file system.
System.IO.DirectoryNotFoundException
Could not find a part of the path 'C:\Users\shawn\AppData\Local\NCrunch\23344\21\EmailAPI.Tests\bin\Debug\netcoreapp2.0\Emails\Templates\BaseEmail.html'
This is the code in the EmailAPI project where the method I am trying to test is making a call to get the content of an html file. This code works fine running the api, but when the unit test gets here, that exception gets thrown.
public void SendEmail(){
var template = System.IO.File.ReadAllText("Emails/Templates/BaseEmail.html")
}
The EmailAPI project structure:
-EmailAPI Project Root
-Emails
-Strategies
-BaseEmailStrategy <--- Class and method under test
-Templates
-BaseEmail.html <--- File read from ReadAllText
-EmailAPI.Tests Project Root
-Emails
-Strategies
-BaseEmailStrategyTests <-- Test class
So in the project being tested, when I do the above code, the application seems to know that I am referencing this file from the root of my project. When the unit test project runs the code when I call the method, it seems to be trying to locate the file path from the NCrunch build output bin folder. Does anybody know how I can get this to work?
I have done some research into using IHostingEnvironment and setting the ContentRoot to the root directory of the EmailAPI project, but am not sure how to get that directory path relative to my EmailAPI.Tests project. I also looked into mocking the file system with System.IO.Abstractions, but I am trying to avoid passing a file system interface into the constructor (because I am using the strategy pattern which makes it a little messy and I want to avoid code changes strictly for testing purposes.) No luck so far though finding a decent solution.