Skip to content

Add Mocking capabilities #26

@mwrock

Description

@mwrock

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions