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

Add Mocking capabilities #26

Closed
mwrock opened this issue Sep 18, 2012 · 8 comments
Closed

Add Mocking capabilities #26

mwrock opened this issue Sep 18, 2012 · 8 comments
Assignees

Comments

@mwrock
Copy link
Member

mwrock commented Sep 18, 2012

So I know I am not the first to ever think or mention this, but having done a deep dive into the Chocolatey testing written by @ferventcoder which uses pester and its own rolled mocks, I now have a picture in my head of how this might look. Something like this:

SYNTAX
Mock [-function <string>] [-mockWith <ScriptBlock>] [-paramFilter [<HashTable>]]
EXAMPLE
Describe "When calling Chocolatey-Install for return value" {
  $script:exec_chocolatey_install_actual = $true
  $expected='dude'
  Mock Chocolatey-Nuget -mockWith { return $expected} -paramFilter @{packageName={$packageName -eq 'testPackage'};version=$Pester.ANY}
  $returnv = Chocolatey-Install 'testpackage'

  It "should return appropriately" {
    $returnv.should.be($expected)
  }

  It "should return as the type that was passed in" {
    $true.should.be($returnv.GetType().Name -eq 'String')
  }  

}

The example uses one of the tests in Chocolatey. It is testing a function called Chocolatey-Install which in turn calls a function Chocolatey-Nuget. The test does not want the real Chocolatey-Nuget which does alot of internet stuff to actually run. The test just wants Chocolatry-Nuget to return the string 'dude' if Chocolatey-Nuget is called with a package name called 'testPackage'.

This would be implemented by Renaming the function called Chocolatey-Nuget to something else and then dynamically creating a function called Chocolatey-Nuget that invokes the mockWith scriptBlock if the param filters are met. At the end of Describe, all renamed mocked functions are renamed back to their original names.

I'd love to submit a pull request for this but wanted to get a discussion started.

@ferventcoder
Copy link
Member

I started a discussion on this somewheres... and that was in email (just looked).

@ferventcoder
Copy link
Member

The email from awhile back... (Late April 2012):

"My recent presentation on pester (April 2012): http://bit.ly/powershellbdd

You may have noticed I started thinking about putting together a mocking framework, but after quite a bit of thought, I think it might be better as part of pester itself. Typically the testing framework and the mocking framework are completely separate, but in the case of powershell it might be better served as one unit. I started looking into it, but before going further, I'd like to get your thoughts."

@mwrock
Copy link
Member Author

mwrock commented Sep 18, 2012

Yeah. I agree that in this case it seems to make more sense to bake directly into Pester. Without a proper DI mechanism which is overkill for PS IMHO, having mocking decoupled from the test framework doesn't make sense.

@ferventcoder
Copy link
Member

I even started some code on this but never went further.

@mwrock
Copy link
Member Author

mwrock commented Sep 18, 2012

Did you start a repo or fork for it? Anything you think is worth starting with. I'm not sure when I would start, I want to wrap up the Chocolatey bootstraper (at least a v.1) before starting anything new but I also see this as a fairly smal effort to get in a working state. Then again, I've said that before embarcing on multi month coding projects where I suddenly wonder why I'm googling for calculus formulas.

Note to self: If you are using calculus in a PS mocking framework, you are doing it wrong.

@ferventcoder
Copy link
Member

I didn't even make it that far.

@ferventcoder
Copy link
Member

I have it sitting uncommitted in a repo - it's not really much at all.

@ghost ghost assigned mwrock Sep 23, 2012
@mwrock
Copy link
Member Author

mwrock commented Oct 22, 2012

This is now commited. with fd0a9fe

@mwrock mwrock closed this as completed Oct 22, 2012
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

2 participants