RE: Mocking when using splatting

95 views
Skip to first unread message

Matt Wrock

unread,
Dec 9, 2013, 10:55:54 AM12/9/13
to Johan Leino, pes...@googlegroups.com
Hi Johan,

Splatting should work just like other params. Try replacing "file" with "path". Get-childitem does not have a param named file and it will blow up when you use that. I think if you use path instead it will work.

Matt

Sent from my Windows Phone

From: Johan Leino
Sent: ‎12/‎9/‎2013 7:38 AM
To: pes...@googlegroups.com
Subject: Mocking when using splatting

I have an issue with mocking Get-ChildItem and using splatting to call the Get-ChildItem function. There's and issue at github for this as well https://github.com/pester/Pester/issues/88#.

Anyway...

Let's say I have a method that looks something like this:

$arguments = @{File=$true} 
if (SomeCondition)
$arguments.Recurse = $true

$arguments.Path = "SomeFile"

return Get-ChildItem @arguments

I would now like to test that Get-ChildItem is called with the correct arguments. So I mock it and:
Assert-MockCalled Get-ChildItem -Exactly 1 {$Recurse -eq $true}

That will blow up saying that 'A parameter cannot be found that matches parameter name 'File'.'

I think it has to do with me using splatting but I don't know...

I belive that the error lies with me trying to call Get-ChildItem using splatting ( @arguments ) when Get-ChildItem is mocked.Splatting tries to match the parameters with key=value as far as I understand and since -File is missing...well

The strange thing is that line 169 in Mock.ps1 ...this one:
$params = [Management.Automation.ProxyCommand]::GetParamBlock($metadata)

doesn't include the -File parameter of Get-ChildItem. To me that is strange...

Any ideas...?

--
You received this message because you are subscribed to the Google Groups "Pester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Johan Leino

unread,
Dec 9, 2013, 2:44:04 PM12/9/13
to pes...@googlegroups.com, Johan Leino
Yeah,
 you are correct. Why on earth I thought that Get-ChildItem had a -File switch I don't know. Maybe I'm too used to intellisense since that parameter showed up in both PowerGUI and PoshTools for VS I was confused.

Thanks!

/ Johan

Matt Wrock

unread,
Dec 9, 2013, 2:49:00 PM12/9/13
to Johan Leino, pes...@googlegroups.com

Weird that intellisense was showing a “File” param. That would be confusing (and wrong). Glad you figured it out.

Dave Wyatt

unread,
Jul 23, 2014, 10:53:13 AM7/23/14
to pes...@googlegroups.com, nahoj...@gmail.com
Get-ChildItem does have a -File switch (in PowerShell 3.0 and later), but it's a dynamic parameter from the FileSystem provider.  The way Mocking currently works may not play well with dynamic parameters (which would severely limit its use for some of PowerShell's core commands.)  I'll look into this at some point.

Matt Wrock

unread,
Jul 23, 2014, 6:06:21 PM7/23/14
to Dave Wyatt, pes...@googlegroups.com, nahoj...@gmail.com
Yup. I *think* that the current implementation will not discover dynamic parameters. Adding that functionality would be great!


Date: Wed, 23 Jul 2014 07:53:13 -0700
From: dlwya...@gmail.com
To: pes...@googlegroups.com
CC: nahoj...@gmail.com

Subject: Re: Mocking when using splatting

For more options, visit https://groups.google.com/d/optout.

Dave Wyatt

unread,
Jul 28, 2014, 3:03:37 PM7/28/14
to pes...@googlegroups.com, dlwya...@gmail.com, nahoj...@gmail.com
I've spent a little bit of time experimenting with this so far.  I think it should be possible to get dynamic parameters working, but it'll be a bit of a headache.  For functions with DynamicParam blocks, it's easy; we just grab that same DynamicParam block and stick it in the mock, the same way we currently grab the CmdletBinding attribute and Param block.
 
When you mock a Cmdlet with dynamic parameters, though, it's more of a pain.  I don't know of any good way that we could extract the logic around those parameters from the cmdlet, so we'd have to execute the .NET code instead.  Something like this: check the cmdlet's implementing type, see if it implements IDynamicParameters.  If so, create an instance, set the properties associated with all of the static parameters first, and call the GetDynamicParameters() method to see what it adds.  Assuming this all works, it should just be a matter of rearranging some of the code a bit to take advantage of this information each time a mock is called.  (Wait to create the ScriptBlock to be executed until the mock is called, instead of doing it when the mock is defined.)

Dave Wyatt

unread,
Aug 31, 2014, 6:07:48 PM8/31/14
to pes...@googlegroups.com, dlwya...@gmail.com, nahoj...@gmail.com
Just merged in code which should fix this problem.  It's not part of a release yet, but can be found in the master branch of the GitHub repo.
For more options, visit <a onmousedown="this.href='https://groups.google.com/groups/opt_out';return true;" onclick="this.href='https://groups.google.com/groups/opt_out';return true;" href="https://groups.google.com/groups/opt_out" target="_bl
...
Reply all
Reply to author
Forward
0 new messages