Correct TestDrive usage

Welcome Forums Pester Correct TestDrive usage

Viewing 5 reply threads
  • Author
    Posts
    • #69157
      Participant
      Topics: 1
      Replies: 18
      Points: 0
      Rank: Member

      Hello,
      I’m trying to test a script which stores some files on hard disk. During the test I would like to store a file to TestDrive:\ and check if it was indeed created. The problem is that file always gets created in “$env:TEMP\myfile.txt”. Could you please explain what am I missing here?

    • #69169
      Participant
      Topics: 28
      Replies: 140
      Points: 59
      Rank: Member

      You do not need to define Get-FilePath again in your test. The mock will take care of that. When you do that, your mock is applying to that function and not the one defined in your script.ps1 file.

    • #69172
      Participant
      Topics: 1
      Replies: 18
      Points: 0
      Rank: Member

      Hi Adam,
      Thanks for reply. If I’m not defining it, I get CommandNotFoundException and if I move script dot-source up, then it creates file in $env:temp and in It block it looks for file in TestDrive.

    • #69175
      Participant
      Topics: 28
      Replies: 140
      Points: 59
      Rank: Member

      Ah. I see. This is where code refactoring comes into play. It seems like you’re just testing if Get-FilePath returns what you expect it to since you don’t have the actual file creation step inside of that function. If this is the case, I suggest remove the Out-File functionality from the script altogether and building a simple test to ensure that Get-FilePath returns the expected string like this:

      If you do need to confirm a function creates a file in a specific location you could also use a mock assertion to ensure Add-Content in this case used the expected path.

      script.ps1

      Then the test would look like this:

    • #69213
      Participant
      Topics: 1
      Replies: 18
      Points: 0
      Rank: Member

      For some reason my previous message was treated as spam. Just wanted to say thank you for help. Everything working smooth now.

    • #69181
      Participant
      Topics: 1
      Replies: 18
      Points: 0
      Rank: Member

      Heh, that makes sense. Ended up with this

      Thank you. Looking forward to your pester talk today. Cheers!

Viewing 5 reply threads
  • The topic ‘Correct TestDrive usage’ is closed to new replies.