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
Unable to fully mock Get-Credential #296
Comments
Well, that's awkward. Here's what a bit of the param block for Get-Credential looks like: [Parameter(ParameterSetName='CredentialSet', Mandatory=$true, Position=0)]
[pscredential]
[System.Management.Automation.CredentialAttribute()]
${Credential}, When you mock a command in Pester, the param block of the original command is copied, and you just replace the implementation. The problem here is that the param block is really doing all the work; for this parameter set, the body of Get-Credential would just return the value of the $Credential parameter. That I have no idea how best to handle this in the general case, at the moment. For your specific example, I'd just not use the Mock command at all, and instead write: function Get-Credential { return @{UserName = 'myuser'} } This way you've got control over the param block, and no nonsense happens. Because you know, in your example, that the only code calling Get-Credential does so without passing any parameters, you don't need a param block anyway. |
I was having the same problem. I've read the issue a couple of times and I think the answer is simpler than stated. I understand @dlwyatt is thinking about how to solve the general problem here, but for me using Mock was still appropriate and worked just fine when expressed as:
That's enough to stop the UI dialog popping and to return a valid object that will work with any subsequent mocking or parameter bindings. |
@cathalmchale It seems to be that this was solved somewhere in a newer release than 3.3.6. Thanks for providing an example of it working |
@cathalmchale Do you happen to remember the version of Pester you were using at the time? I'm still seeing this same issue in 4.10.1 |
@gerron The version was 4.7.2. I'd say you're mock isn't getting used and so you get the same net effect - credential dialog pops. I most often see this when I'm mocking within modules and have multiple modules involved. You need to use InModuleScope or -Module "ModuleName" for your mocks and make sure that the mock is declared for the right module - the one that will be executing the cmdlet. |
@cathalmchale I'm mocking within modules with only one module being involved. I'm currently using InModuleScope with the module specified and no luck. Every other cmdlet mock is works such as Invoke-RestMethod except Get-Credential. |
@gerron is the code publicly available on github? I can have a look if you like and see if anything jumps out. |
@cathalmchale Thanks a lot for taking a look. Much appreciated! https://gist.github.com/gerron/835e0a082aa4bd630ae91b5eab383e1e |
While you can get around this by wrapping the call to
Get-Credential
in another function, I figured it was worth mentioning. In version 3.3.6 I can't seem to fully mock the call toGet-Credential
without the credential dialog still opening up.This test file illustrates what I mean:
This test will pass if you enter any value into the credential dialog and click OK, but will fail if you hit cancel. Obviously in either case it blocks the tests until some interaction occurs.
Perhaps this can't be stopped, but if so, it might be worth mentioning it somewhere.
The text was updated successfully, but these errors were encountered: