@@ -5,170 +5,142 @@ BeforeAll {
55}
66
77Describe ' Mock an environment variable' {
8- Describe ' Code is called' {
9- BeforeAll {
10- $script :EnvironmentVariableName = " test$ ( New-Guid ) "
11- $script :InitialValue = ' Some value here and there'
12- }
8+ BeforeAll {
9+ $EnvironmentVariableName = " test$ ( New-Guid ) "
10+ $InitialValue = ' Some value here and there'
11+ }
1312
14- It ' Code is called' {
15- $script :called = $false
16- Mock- EnvironmentVariable - Variable $environmentVariableName - Value $InitialValue {
17- $script :called = $true
18- }
19- $script :called | Should - BeTrue
20- }
21-
22- AfterAll {
23- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
13+ It ' Code is called' {
14+ $script :called = $false
15+ Mock- EnvironmentVariable - Variable $environmentVariableName - Value $InitialValue {
16+ $script :called = $true
2417 }
18+ $called | Should - BeTrue
2519 }
2620
27- Describe ' Environment variable is set up' {
28- BeforeAll {
29- $script :environmentVariableName = " test$ ( New-Guid ) "
30- $script :InitialValue = ' Some value here and there'
31- }
32-
33- It ' Environment variable is set up' {
34- [Environment ]::GetEnvironmentVariable($EnvironmentVariableName ) | Should - Be $null - Because ' environment variable should be reset'
35- Mock- EnvironmentVariable - Variable $environmentVariableName - Value $InitialValue {
36- [Environment ]::GetEnvironmentVariable($EnvironmentVariableName ) | Should - Be $InitialValue
37- }
38- [Environment ]::GetEnvironmentVariable($EnvironmentVariableName ) | Should - Be $null - Because ' environment variable should be reset'
39- }
21+ AfterAll {
22+ [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
23+ }
24+ }
4025
41- AfterAll {
42- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
43- }
26+ Describe ' Check env mocking' - ForEach @ (
27+ @ {
28+ InitialValue = ' Initial Value' ;
29+ MockedValue = ' Mocked Value' ;
30+ ValueInsideTheScript = ' Mocked Value' ;
31+ Script = { }
4432 }
45-
46- Describe ' Environment variable is set' {
47- BeforeAll {
48- $script :environmentVariableName = " test$ ( New-Guid ) "
49- $script :InitialValue = ' Some value here and there'
50- $script :UpdatedValue = ' Some updated value'
51- [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
52- }
53- It ' Environment variable is set up' {
54- Mock- EnvironmentVariable - Variable $environmentVariableName - Value $UpdatedValue {
55- [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $UpdatedValue
56- }
57- [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $InitialValue
58- }
59- AfterAll {
60- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
61- }
33+ @ {
34+ InitialValue = $null ;
35+ MockedValue = ' Mocked Value' ;
36+ ValueInsideTheScript = ' Mocked Value' ;
37+ Script = { }
6238 }
63-
64- Describe ' Restore variable that got destroyed' {
65- BeforeAll {
66- $script :environmentVariableName = " test$ ( New-Guid ) "
67- $script :InitialValue = ' Some value here and there'
68- $script :UpdatedValue = ' Some updated value'
69-
70- [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
71- Mock- EnvironmentVariable - Variable $environmentVariableName - Value $UpdatedValue {
72- [Environment ]::SetEnvironmentVariable($environmentVariableName , $null )
73- }
74- }
75- It ' Environment variable is set up' {
76- [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $InitialValue
77- }
78- AfterAll {
79- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
80- }
39+ @ {
40+ InitialValue = ' Initial Value' ;
41+ MockedValue = $null ;
42+ ValueInsideTheScript = ' Initial Value' ;
43+ Script = { }
8144 }
82-
83- Describe ' Use the original value if value is not set up' {
84- BeforeAll {
85- $script :environmentVariableName = " test$ ( New-Guid ) "
86- $script :environmentVariable = " env:${environmentVariableName} "
87- $script :InitialValue = ' Some value here and there'
88-
89- [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
90- }
91- It ' Test' {
92- Mock- EnvironmentVariable - Variable $environmentVariableName {
93- [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $InitialValue
94- }
95- }
96- AfterAll {
97- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
98- }
45+ @ {
46+ Script = { }
9947 }
100-
101- Describe ' Do not create the variable if no value is specified' {
102- BeforeAll {
103- $script :environmentVariableName = " test$ ( New-Guid ) "
104- }
105- It ' Test' {
106- Mock- EnvironmentVariable - Variable $environmentVariableName {
107- [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $null
108- }
109- }
110- AfterAll {
111- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
48+ @ {
49+ InitialValue = ' Initial Value' ;
50+ MockedValue = ' Mocked Value' ;
51+ ValueInsideTheScript = ' Mocked Value' ;
52+ Script = {
53+ [Environment ]::SetEnvironmentVariable($args [0 ], ' Some updated value' )
11254 }
11355 }
114-
115- Describe ' Initial value should be reasigned' {
116- BeforeAll {
117- $script :environmentVariableName = " test$ ( New-Guid ) "
118- $script :InitialValue = ' Some value here and there'
119- $script :UpdatedValue = ' Some updated value'
120- $script :Message = ' Some message here and there'
121- [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
56+ @ {
57+ InitialValue = ' Initial Value' ;
58+ MockedValue = $null ;
59+ ValueInsideTheScript = ' Initial Value' ;
60+ Script = {
61+ [Environment ]::SetEnvironmentVariable($args [0 ], ' Some updated value' )
12262 }
123- It ' Throw' {
124- { Mock- EnvironmentVariable `
125- - Variable $environmentVariableName `
126- - Value $UpdatedValue { throw $Message }
127- } | Should - Throw - ExpectedMessage $Message
128- [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $InitialValue
63+ }
64+ @ {
65+ InitialValue = $null ;
66+ MockedValue = ' Mocked Value' ;
67+ ValueInsideTheScript = ' Mocked Value' ;
68+ Script = {
69+ [Environment ]::SetEnvironmentVariable($args [0 ], ' Some updated value' )
12970 }
130-
131- AfterAll {
132- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
71+ }
72+ @ {
73+ InitialValue = $null ;
74+ MockedValue = $null ;
75+ ValueInsideTheScript = $null ;
76+ Script = {
77+ [Environment ]::SetEnvironmentVariable($args [0 ], ' Some updated value' )
13378 }
13479 }
80+ ) {
81+ BeforeAll {
82+ $environmentVariableName = " test_$ ( New-Guid ) "
83+ [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
84+ }
85+ It ' Test' {
86+ Mock- EnvironmentVariable `
87+ - Variable $environmentVariableName `
88+ - Value $MockedValue {
89+ [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $ValueInsideTheScript
90+ Invoke-Command - ScriptBlock $script - ArgumentList $environmentVariableName
91+ }
92+ [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $InitialValue
93+ }
94+ AfterAll {
95+ [Environment ]::SetEnvironmentVariable($environmentVariableName , $null )
96+ }
97+ }
13598
136- Describe ' Created variable should be destroyed' {
137- BeforeAll {
138- $script :environmentVariableName = " test$ ( New-Guid ) "
139- $script :environmentVariable = " env:${environmentVariableName} "
140- $script :InitialValue = ' Some value here and there'
141- $script :UpdatedValue = ' Some updated value'
142- $script :Message = ' Some message here and there'
99+ Describe ' Should throw' - ForEach @ (
100+ @ {
101+ InitialValue = ' Initial Value' ;
102+ MockedValue = ' Mocked Value' ;
103+ Script = {
104+ throw ' Some exception here and there'
143105 }
144- It ' Throw ' {
145- { Mock - EnvironmentVariable `
146- - Variable $environmentVariableName `
147- - Value $UpdatedValue { throw $Message }
148- } | Should - Throw - ExpectedMessage $Message
149- [ Environment ]::GetEnvironmentVariable( $environmentVariableName ) | Should - Be $null
106+ }
107+ @ {
108+ InitialValue = ' Initial Value ' ;
109+ MockedValue = $null ;
110+ Script = {
111+ throw ' Some exception here and there '
150112 }
151-
152- AfterAll {
153- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
113+ }
114+ @ {
115+ InitialValue = $null ;
116+ MockedValue = ' Mocked Value' ;
117+ Script = {
118+ throw ' Some exception here and there'
154119 }
155120 }
156-
157- Describe ' Set up environment variable is cleared up' {
158- BeforeAll {
159- $environmentVariableName = " test$ ( New-Guid ) "
121+ @ {
122+ InitialValue = $null ;
123+ MockedValue = $null ;
124+ Script = {
125+ throw ' Some exception here and there'
160126 }
161-
162- It ' Set up environment variable is cleared up' {
127+ }
128+ ) {
129+ BeforeAll {
130+ $environmentVariableName = " test_$ ( New-Guid ) "
131+ [Environment ]::SetEnvironmentVariable($environmentVariableName , $InitialValue )
132+ }
133+ It ' Test' {
134+ {
163135 Mock- EnvironmentVariable `
164- - Variable $environmentVariableName {
165- New-Item - Path $environmentVariable - Value ' Some value'
136+ - Variable $environmentVariableName `
137+ - Value $MockedValue {
138+ Invoke-Command - ScriptBlock $script - ArgumentList $environmentVariableName
166139 }
167- [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $null
168- }
169-
170- AfterAll {
171- [Environment ]::SetEnvironmentVariable($EnvironmentVariableName , $null )
172- }
140+ } | Should - Throw
141+ [Environment ]::GetEnvironmentVariable($environmentVariableName ) | Should - Be $InitialValue
142+ }
143+ AfterAll {
144+ [Environment ]::SetEnvironmentVariable($environmentVariableName , $null )
173145 }
174146}
0 commit comments