0

I have a method. In it I am retrieving a file and reading its bytes. Its a certificate file. var readAllBytes = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/") + "Certificates\\Mynew.pfx");

MethodA()
{
  var readAllBytes = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/") + "Certificates\\Mynew.pfx");`
   ...
 }

I am writing a unit test for this MethodA. As you can see when I hit the readAllBytes line it exceptions out. How do I over-come this. Please let me know if you need any info.

Object reference not set to an instance of an object.

Since I dont have the file path when my debug point reaches that line it throws up the above exception.

RookieAppler
  • 1,517
  • 5
  • 22
  • 58
  • Please add exception details to the question. – Zoran Horvat Dec 23 '16 at 23:25
  • @ZoranHorvat. Added the details – RookieAppler Dec 23 '16 at 23:33
  • 1
    Extract dependencies that cross system boundaries (database, time, file system, ...) to services that can be substituted for stubs through Dependency Injection. – Jeroen Vannevel Dec 23 '16 at 23:39
  • I used interface and injected it in my constructor. I was able to use path.combine and get the path on my local Development machine. Unit test worked on my local. When I put it on local though it failed as the path there was different. I mean the project was in folder1\folder2\folder3\folder4 for ex. On my devlopment machine it was just folder1\folder2. I read somewhere to make 'Mynew.pfx' file to 'Copy to Output' directory. Did that but how do i go about the path now? – RookieAppler Dec 29 '16 at 18:49

0 Answers0