Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Created March 9, 2019 07:31
Show Gist options
  • Save nohwnd/de656a9bd8d74281ee0c778113619f8e to your computer and use it in GitHub Desktop.
Save nohwnd/de656a9bd8d74281ee0c778113619f8e to your computer and use it in GitHub Desktop.
Share data among tests safely
# GOOD:
# shares data among tests without polluting
# the script scope
Describe "I share data" {
$container = @{ Value = 1 }
It "I give data" {
$container.Value = 5
}
It "I get data" {
$container.Value | Should -Be 5
}
}
Describe "I don't want any data" {
It "does not get data from the previous describe" {
$container.Value | Should -Be $null
}
}
# BAD:
# shares data witih the describe but
# also leaks it to all other tests
Describe "I share data" {
$script:value = 1
It "I give data" {
$script:value = 5
}
It "I get data" {
$script:value | Should -Be 5
}
}
Describe "I don't want any data" {
It "does not get data from the previous describe" {
# yikes, data leaked here making this test fail
$script:value | Should -Be $null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment