Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Pester to see custom Should operators? #590

Closed
kamranayub opened this issue Aug 12, 2016 · 17 comments
Closed

How to get Pester to see custom Should operators? #590

kamranayub opened this issue Aug 12, 2016 · 17 comments

Comments

@kamranayub
Copy link

I must be missing something simple because I feel really close.

I have defined a new PesterBeDate function and failure functions. According to what I see in the Should assertion source, it should be able to find my command. In fact if I run:

Get-Command PesterBeDate -CommandType Function

I see the command output. But for some reason Pester doesn't:

RuntimeException: 'BeDate' is not a valid Should operator.
   at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.2\Functions\Assertions\Should.ps1: line 42
@dlwyatt
Copy link
Member

dlwyatt commented Aug 12, 2016

Just put your function into a PS1 file in the Pester\Functions\Assertions directory, and reload the module. Should be all set. :)

@kamranayub
Copy link
Author

kamranayub commented Aug 12, 2016

Ah, but this for an OSS project... Is there a way to source Pester not from
global? Or any other way to "extend" Pester so people don't need to do custom stuff to contribute?

On Fri, Aug 12, 2016, 12:31 Dave Wyatt [email protected] wrote:

Just put your function into a PS1 file in the Pester\Functions\Assertions
directory, and reload the module. Should be all set. :)


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#590 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAiaa6w7FzAGQOLifRszwyRvegrBDHi7ks5qfK3WgaJpZM4JjSwc
.

@dlwyatt
Copy link
Member

dlwyatt commented Aug 16, 2016

It probably depends on where you've defined those functions. Unless they're in the global scope, Pester's calls to Get-Command aren't going to find them. (The way that Should operators get loaded is also changing in v4.)

However, for now, you can inject your functions into Pester's scope after loading the module and it should work:

$pesterModule = Import-Module Pester -PassThru

. $pesterModule {
    function PesterBeDate { }
    # etc
}

Edit: I'm not 100% certain whether you need to use the . operator here, but I changed the code to be on the safe side. Otherwise the functions might be discarded when your script block goes out of scope.

@kamranayub
Copy link
Author

Thanks, I did end up sourcing a local module for better out of the box
tests for contributors. Then just copying any assertions after the fact and
that works fine.

On Mon, Aug 15, 2016, 20:24 Dave Wyatt [email protected] wrote:

It probably depends on where you've defined those functions. Unless
they're in the global scope, Pester's calls to Get-Command aren't going to
find them. (The way that Should operators get loaded is also changing in
v4.)

However, for now, you can inject your functions into Pester's scope after
loading the module and it should work:

$pesterModule = Import-Module Pester -PassThru
& $pesterModule {
function PesterBeDate { }
# etc
}


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#590 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAiaawbkEid_M7QpDyudtggN4qsPzZq2ks5qgRFOgaJpZM4JjSwc
.

@kamranayub
Copy link
Author

@ryangribble
Copy link

(The way that Should operators get loaded is also changing in v4.)

Hey guys, is there any information about how these have changed in v4? Ive been unsuccesful in located instructions on how to do custom Should extensions in the new version?

@dlwyatt
Copy link
Member

dlwyatt commented Mar 30, 2017

Didn't write instructions yet, will get to that this weekend. However, you can look at https://github.com/pester/Pester/blob/master/Functions/Assertions/Be.ps1 for guidance. Three main changes:

  • You have to call Add-AssertionOperator to register your operator with Pester.
  • Your test function now has to return an object with Succeeded (boolean) and FailureMessage (string) properties. FailureMessage is ignored unless Succeeded is $false.
  • Your test function, at a minimum, must take three parameters: $ActualValue, $ExpectedValue, [switch] $Negate. -Negate is the equivalent of what we used to do with separate functions (PesterBeFailureMessage and NotPesterBeFailureMessage, etc.) If you add any other named parameters to your test function, they'll show up as dynamic parameters when Should is called with your operator.

The names of your functions don't matter anymore. You can add aliases for your operator with the -Alias parameter on Add-AssertionOperator. If your operator can handle entire arrays, you can add the -SupportsArrayInput switch, and anything that gets piped to Should will be passed in as an array to a single call of your operator in the $ActualValue parameter. If you don't use -SupportsArrayInput, then piped in elements to Should will get tested one at a time, same as how everything worked in v3.

@dlwyatt
Copy link
Member

dlwyatt commented Mar 30, 2017

Actually, Be.ps1 is probably one of the more complex files. Here's a simple one: https://github.com/pester/Pester/blob/master/Functions/Assertions/BeGreaterThan.ps1

@ryangribble
Copy link

ryangribble commented Mar 31, 2017

Thanks for the info @dlwyatt

It doesn't seem like Add-AssetionOperator is an exported command though? Running version 4.0.3

PS C:\Users\ryan.gribble> Get-Command -Module Pester -Name Add-*

<nothing>



PS C:\Users\ryan.gribble> Get-Command -Module Pester

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        AfterAll                                           4.0.3      pester
Function        AfterEach                                          4.0.3      pester
Function        AfterEachFeature                                   4.0.3      pester
Function        AfterEachScenario                                  4.0.3      pester
Function        Assert-MockCalled                                  4.0.3      pester
Function        Assert-VerifiableMocks                             4.0.3      pester
Function        BeforeAll                                          4.0.3      pester
Function        BeforeEach                                         4.0.3      pester
Function        BeforeEachFeature                                  4.0.3      pester
Function        BeforeEachScenario                                 4.0.3      pester
Function        Context                                            4.0.3      pester
Function        Describe                                           4.0.3      pester
Function        Find-GherkinStep                                   4.0.3      pester
Function        Get-MockDynamicParameters                          4.0.3      pester
Function        Get-TestDriveItem                                  4.0.3      pester
Function        GherkinStep                                        4.0.3      pester
Function        In                                                 4.0.3      pester
Function        InModuleScope                                      4.0.3      pester
Function        Invoke-Gherkin                                     4.0.3      pester
Function        Invoke-GherkinStep                                 4.0.3      pester
Function        Invoke-Mock                                        4.0.3      pester
Function        Invoke-Pester                                      4.0.3      pester
Function        It                                                 4.0.3      pester
Function        Mock                                               4.0.3      pester
Function        New-Fixture                                        4.0.3      pester
Function        New-MockObject                                     4.0.3      pester
Function        New-PesterOption                                   4.0.3      pester
Function        SafeGetCommand                                     4.0.3      pester
Function        Set-DynamicParameterVariables                      4.0.3      pester
Function        Set-TestInconclusive                               4.0.3      pester
Function        Setup                                              4.0.3      pester
Function        Should                                             4.0.3      pester

@dlwyatt
Copy link
Member

dlwyatt commented Mar 31, 2017

That's correct, though there's a PR open right now to change that.

@stuartleeks
Copy link

I've just installed 4.0.4 to try to update a couple of libraries with custom assertions and am hitting this issue. I can't see Add-AssertionOperator

@nohwnd
Copy link
Member

nohwnd commented Jul 24, 2017

#765 merged need to release new version

@stuartleeks
Copy link

Thanks @nohwnd. Do you have any idea of timelines for a new release now that this is merged?

@it-praktyk
Copy link
Contributor

@stuartleeks , please read #793.

@stuartleeks
Copy link

Thanks @it-praktyk, I read through #793 when I was trying to track down my issue. I guess the questions I was really asking is whether there will be a release ahead of a final v4 release (as there still seems to be a list of issues before then) :-)

@nohwnd
Copy link
Member

nohwnd commented Jul 25, 2017

@stuartleeks it's released as 4.0.5. Closing this issue, feel free to reopen if you need more help.

@nohwnd nohwnd closed this as completed Jul 25, 2017
@stuartleeks
Copy link

Awesome, thanks @nohwnd !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants