Test non-idempotent web methods with Pester

109 views Asked by At

I'm attempting to test a PowerShell command (Set-PropertyValue) that sets a property value of a SharePoint list item. The function use the Invoke-WebMethod function do to the actual work.

My current Set-PropertyValue.Test.ps1 file:

# include stuff omitted

Describe 'Set-PropertyValue' {

  #
  # arrange
  #

  # set $url, $list, $id, $property variables
  ...

  # the value that it should be 
  $expected=10.5

  BeforeEach {
    # get the property's current value
    $original = Get-PropertyValue $url $list $id $property
  }

  AfterEach {
    # restore original value
    Set-PropertyValue $url $list $id $property $original
  }

  It "Should set a property's value" {

    #
    # act
    #

    # update property's value
    $response = Set-PropertyValue $url $list $id $property $expected

    # get the new value
    $actual = Get-PropertyValue $url $list $id $property

    #
    # assert
    #

    $response.statuscode | should be 204 # no content
    $actual | Should Be $expected

  } # It

} # Describe

I don't like this for a number of reasons:

  • external dependence upon Get-PropertyValue
  • no test isolation; changes are made to SharePoint list
  • potential to level list item in undesirable state
  • test not structured to test multiple properties easily, in a loop perhaps

Is there a better way to test this?

1

There are 1 answers

0
craig On

One approach:

  • place all command scripts in a subdirectory (e.g Functions)
  • create a module file (e.g. PsFoo.psm1); 'dot-source' each of the command's script files (e.g. Functions\Invoke-Foo.ps1), but exclude the unit-test file (e.g. Functions\Invoke-Foo.Tests.ps1).
  • create a module test file (e.g. PsFoo.Tests.ps1) that 'dot-sources' the module file and contains all of the integration tests.
  • refactor all unit tests that exercise more than one command (like my question's example) into an integration test
  • PS> Invoke-Pester will run the integration tests (in PsFoo.Tests.ps1) and the unit tests (in Functions\*.Tests.ps1).