Pester exception test is failing

Problem:

My Pester testing if my code throws is failing even though the code is throwing the exception.

Solution:

Make sure the block of code is surrounded with {}.

Explanation:

I originally wrote the test like this:

Describe "Set-WorkingDirectory" {

   Context "with invalid dir" {

      It "throws" {

         Set-WorkingDirectory -dir "hello world" | Should Throw

      }

   }

}

The test failed showing the message of the exception thrown but not passing.

Simply wrapping the code before the pipe with {} yielded the desired result.

Describe "Set-WorkingDirectory" {

   Context "with invalid dir" {

      It "throws" {

         { Set-WorkingDirectory -dir "hello world" } | Should Throw

      }

   }

}

Add comment

Loading