forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pester.Mock.RSpec.ts.ps1
1222 lines (1002 loc) · 44.6 KB
/
Pester.Mock.RSpec.ts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
param ([switch] $PassThru, [switch] $NoBuild)
Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
if (-not $NoBuild) { & "$PSScriptRoot\..\build.ps1" }
Import-Module $PSScriptRoot\..\bin\Pester.psd1
$global:PesterPreference = [PesterConfiguration] @{
Debug = @{
ShowFullErrors = $true
WriteDebugMessages = $true
WriteDebugMessagesFrom = "Mock"
ReturnRawResultObject = $true
}
Output = @{ Verbosity = 'Normal' }
}
i -PassThru:$PassThru {
b "basic mocking in RSpec Pester" {
t "running a single mock in one It" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
It 'i1' {
Mock f { "mock" }
f
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Tests[0].StandardOutput | Verify-Equal "mock"
}
t "mock does not leak into the subsequent It" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
It 'i1' {
Mock f { "mock" }
f
}
It 'i2' {
f
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Tests[0].StandardOutput | Verify-Equal "mock"
$actual.Containers[0].Blocks[0].Tests[1].StandardOutput | Verify-Equal "real"
}
t "mock defined in beforeall is used in every it" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
BeforeAll {
Mock f { "mock" }
}
It 'i1' {
f
}
It 'i2' {
f
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Tests[0].StandardOutput | Verify-Equal "mock"
$actual.Containers[0].Blocks[0].Tests[1].StandardOutput | Verify-Equal "mock"
}
t "mock defined in beforeall is counted independently" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
BeforeAll {
Mock f { "mock" }
}
It 'i1' {
f
Should -Invoke f -Times 1 -Exactly
}
It 'i2' {
f
Should -Invoke f -Times 1 -Exactly
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Tests[0].Passed | Verify-True
$actual.Containers[0].Blocks[0].Tests[1].Passed | Verify-True
}
t "mock defined in before all can be counted from all tests with -Describe" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
BeforeAll {
Mock f { "mock" }
}
It 'i1' {
f
Should -Invoke f -Times 1 -Exactly
}
It 'i2' {
f
Should -Invoke f -Times 2 -Exactly -Scope Describe
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Tests[0].Passed | Verify-True
$actual.Containers[0].Blocks[0].Tests[1].Passed | Verify-True
}
t "mock defined in before all can and counted from after all automatically counts all calls in the current block" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
BeforeAll {
Mock f { "mock" }
}
It 'i1' {
f
}
It 'i2' {
f
}
AfterAll {
Should -Invoke f -Times 2 -Exactly
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Tests[0].Passed | Verify-True
$actual.Containers[0].Blocks[0].Tests[1].Passed | Verify-True
}
}
b "taking mocks from all scopes" {
t "mocks defined in the parent scope can still be used" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
BeforeAll {
Mock f { "mock" }
}
Describe 'd2' {
Describe 'd3' {
It 'i1' {
f
}
}
}
AfterAll {
Should -Invoke f -Times 1 -Exactly
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Blocks[0].Blocks[0].Tests[0].StandardOutput | Verify-Equal 'mock'
}
}
b "mock filters" {
t "calling filtered and default mock chooses the correct mock to call" {
$sb = {
BeforeAll { function f { "real" } }
Describe 'd1' {
BeforeAll {
Mock Get-Variable { "filtered" } -ParameterFilter {
$Name -eq 'PSVersionTable' -and $ValueOnly
}
Mock Get-Variable { "default" }
}
It 'makes a call to the filtered mock' {
Get-Variable -Name PSVersionTable -ValueOnly | Should -Be "filtered"
}
It 'makes a call to the default mock' {
Get-Variable -Name PSVersionTable | Should -Be "default"
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Tests[0].Passed | Verify-True
$actual.Containers[0].Blocks[0].Tests[1].Passed | Verify-True
}
}
b "named mock scopes" {
t "asserting in scope describe finds all mocks in the nearest describe" {
$sb = {
BeforeAll { function a { } }
Describe 'd-2' {
# scope 4
Describe "d-1" {
# scope 3
Describe 'd1' {
# scope 2
BeforeAll {
Mock a { }
}
It 'i1' {
a # call 1
}
Context "c1" {
# scope 1
It 'i2' {
a # call 2
}
It 'i1' {
# scope 0
Should -Invoke a -Exactly 0 -Scope 0
Should -Invoke a -Exactly 0 -Scope It
Should -Invoke a -Exactly 1 -Scope 1
Should -Invoke a -Exactly 1 -Scope Context
Should -Invoke a -Exactly 2 -Scope 2
Should -Invoke a -Exactly 2 -Scope Describe
Should -Invoke a -Exactly 2 -Scope 3
Should -Invoke a -Exactly 2 -Scope 4
}
}
}
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Blocks[0].Blocks[0].Blocks[0].Tests[1].Passed | Verify-True
}
t "asserting in scope describe finds all mocks in the nearest describe even when it is more than 2 levels away" {
# https://github.com/pester/Pester/issues/1833
# there is special logic that handles the first two levels in easy way,
# the next levels were off by one
$sb = {
Describe 'd2' {
# scope 5
BeforeAll {
function a { }
Mock a {}
# calling it 3 times, this should not be reached
# we should search only till Describe that is nearest
# to the test, which is Describe d1
a
a
a
}
Describe 'd1' {
# scope 4
BeforeAll {
a
}
Context 'c3' {
# scope 3
Context 'c2' {
# scope 2
Context 'c1' {
# scope 1
It 'i1' {
# scope 0
Should -Invoke a -Exactly 0 -Scope 0
# checking by name
Should -Invoke a -Exactly 1 -Scope Describe
# double checking by scope number
Should -Invoke a -Exactly 1 -Scope 4
# make sure we would fail if we searched too low or too high
Should -Invoke a -Exactly 0 -Scope 3
Should -Invoke a -Exactly (3 + 1) -Scope 5
}
}
}
}
}
}
}
$actual = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true } })
$actual.Containers[0].Blocks[0].Blocks[0].Blocks[0].Blocks[0].Blocks[0].Tests[0].Passed | Verify-True
}
}
b "should in mock" {
t "should will throw even when Should ErrorAction preference is set to Continue and fails the test" {
$sb = {
Describe "a" {
It "it" {
function f ($Name) { "real function" }
Mock f -MockWith { "default mock" }
Mock f -ParameterFilter { $Name | Should -Be "a" } -MockWith { "mock with filter" }
f "b"
"won't reach this"
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{
ScriptBlock = $sb
PassThru = $true
}
Should = @{
ErrorAction = 'Continue'
}
Output = @{
CIFormat = 'None'
}
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.StandardOutput | Verify-Null # the "won't reach this" should not run because the mock filter will throw before it
$err = $t.ErrorRecord[0] -split "`n"
$err[-3] | Verify-Equal "Expected: 'a'"
$err[-2] | Verify-Equal "But was: 'b'"
}
}
b "splatting on default params" {
t "should be able to splat whatif" {
# https://github.com/pester/Pester/issues/1519
$sb = {
Describe "a" {
It "it" {
function Subject {
$params = @{
Path = 'c:\temp\nothing'
WhatIf = $true
}
Remove-Item @params
}
Mock Remove-Item {
'This should be called'
}
Subject
Should -Invoke Remove-Item -Exactly 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.StandardOutput | Verify-Equal 'This should be called'
$t.Result | Verify-Equal "Passed"
}
}
b "cross module mocking and counting" {
try {
Get-Module Source, Target | Remove-Module
New-Module -Name Source -ScriptBlock {
# this module is the source of the public command
# we should be able to mock it in a different module
# in user scope
# and in this module as well
function Public {
Private
}
function Private {
"private"
}
Set-Alias -Name pub -Value Public
Export-ModuleMember -Function Public -Alias pub
} | Import-Module -Force
New-Module Target {
# this module is the target we will inject mock into it
# and check if the call to Public2 is calling the mocked
# Public function imported from Source
function Public2 {
Public
}
} | Import-Module -Force
t "can mock Private in the same module" {
$sb = {
Describe "a" {
It "it" {
Mock -ModuleName Source -CommandName Private -MockWith { "mock" }
Public
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.StandardOutput | Verify-Equal 'mock'
$t.Result | Verify-Equal "Passed"
}
# we have two modules one is the one that defines the function we are mocking
# and the other one is using it. To mock the public functions of Source we need to insert
# mocks into the Target module (because that is the module using them).
# we also need to ensure that the mock body will still run in the test scope.
t "can mock Public in a module that is not the module that defines that function" {
$sb = {
Describe "a" {
It "it" {
Mock -ModuleName Target -CommandName Public -MockWith { "mock" }
Public2
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.StandardOutput | Verify-Equal 'mock'
$t.Result | Verify-Equal "Passed"
}
t "can mock Public in a module that is not the module that defines that function and still runs the MockWith in the test scope" {
$sb = {
Describe "a" {
It "it" {
$mockResult = "mmm"
Mock -ModuleName Target -CommandName Public -MockWith { $mockResult }
Public2
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.StandardOutput | Verify-Equal 'mmm'
$t.Result | Verify-Equal "Passed"
}
t "can count calls to Public in a module that is not the module that defines that function and still run the MockWith in the test scope" {
# here we have three session states at play.
# - The caller session state in which we want to invoke the MockWith, to be able to use variables from the test in the mock body
# - The target session state in which we will insert the mock, in this case the Target module Session State
# - And the source session state (or more precisely the Source module) from which we resolve the command, and need to define fully qualified name for it
# and it's aliases to point to Source/Public
#
# the Should -Invoke part should then find the calls that were made from the Target module, to Target||Public function
$sb = {
Describe "a" {
It "it" {
$mockResult = "mmm"
Mock -ModuleName Target -CommandName Public -MockWith { $mockResult }
Public2 | Should -Be "mmm"
Should -Invoke Public -ModuleName Target -Exactly 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
t "can mock Private in a module and then call it from a different module" {
$sb = {
Describe "a" {
It "it" {
Mock -ModuleName Source -CommandName Private -MockWith { "mock" }
Public2 # -> Public -> Private (mocked)
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.StandardOutput | Verify-Equal 'mock'
$t.Result | Verify-Equal "Passed"
}
t "can count calls to Private in a module when called from a different module" {
$sb = {
Describe "a" {
It "it" {
Mock -ModuleName Source -CommandName Private -MockWith { "mock" }
Public2 | Should -Be 'mock'
Should -Invoke Private -ModuleName Source
}
It "cleans up the mock" {
Public2 Should -Not -Be 'mock'
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$r.Containers[0].Blocks[0].Tests[0].Result | Verify-Equal "Passed"
$r.Containers[0].Blocks[0].Tests[1].Result | Verify-Equal "Passed"
}
}
finally {
Get-Module Source, Target | Remove-Module -Force -ErrorAction Ignore
}
}
b "counting verifiable mocks" {
t "should count mocks correctly when there are multiple behaviors defined in the test" {
# https://github.com/pester/Pester/issues/1539
$sb = {
Context "a" {
It "b" {
function a {}
function b {}
Mock a
Mock a -ParameterFilter { $true }
Mock b -Verifiable
a
b
Should -InvokeVerifiable
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
t "should count mocks correctly when there are multiple behaviors defined in block" {
# https://github.com/pester/Pester/issues/1539
$sb = {
Context "a" {
BeforeAll {
function a {}
function b {}
Mock a
Mock a -ParameterFilter { $true }
Mock b -Verifiable
}
It "b" {
a
b
Should -InvokeVerifiable
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
}
b "top-level mocks" {
t "should allow mock to be defined in top-level BeforeAll" {
# https://github.com/pester/Pester/issues/1559
$sb = {
BeforeAll {
Mock Get-Command { "ffff" }
Get-Command | Should -Be "ffff"
}
Describe "d1" {
It "i1" {
Get-Command | Should -Be "ffff"
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
t "should count mock in top-level BeforeAll" {
# https://github.com/pester/Pester/issues/1559
$sb = {
BeforeAll {
Mock Get-Command { "ffff" }
Get-Command | Should -Be "ffff"
Should -Invoke Get-Command -Exactly 1
}
Describe "d1" {
It "i1" {
Get-Command | Should -Be "ffff"
Should -Invoke Get-Command -Exactly 1
Should -Invoke Get-Command -Exactly 1 -Scope Describe
Should -Invoke Get-Command -Exactly 2 -Scope 2
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
t "should count verifiable mock in top-level BeforeAll" {
# https://github.com/pester/Pester/issues/1559
$sb = {
BeforeAll {
Mock Get-Command { "ffff" } -Verifiable
Get-Command | Should -Be "ffff"
Should -InvokeVerifiable
}
Describe "d1" {
It "i1" {
Should -InvokeVerifiable
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
}
b "mocking cmdlets" {
t "mocking Test-Path" {
# https://github.com/pester/Pester/issues/1551
# Test-Path was not always taking from SafeCommands, so cleaning TestDrive
# failed. This is why we need to be in extra Context, to clean up TestDrive
# not just tear it down.
$sb = {
Describe Do-Something {
BeforeAll {
function Do-Something { }
Mock Test-Path { $true }
}
Context 'Some block' {
It 'does something' {
Do-Something
}
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
}
b "clean up mocks when -ModuleName is used" {
t "cleans up Mock in the module where it was defined" {
# when module name is used we should clean up in the module in which we defined the mock
# command and aliased, and not in the caller scope
# https://github.com/pester/Pester/issues/1693
Get-Module m | Remove-Module
$m = New-Module -Name m {
# calling 'a' which calls 'f' so we run the mock that is effective
# inside of this module
function a () { f }
function f () { "real" }
Export-ModuleMember -Function a
} -PassThru
$m | Import-Module
$sb = {
BeforeAll {
Mock f -ModuleName m { "mock" }
}
Describe "d1" {
It "i1" {
# this should mock and then on the end the mock should be teared down
# correctly so it is no longer effective in the next test run, when this is
# done incorrectly, the alias and mock function remain defined in the module
# and the mock hook remains in place. This breaks mock counting in subsequent tests.
a | Should -Be "mock"
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$r.Containers[0].Blocks[0].Tests[0].Result | Verify-Equal "Passed"
$command = & ($m) { Get-Command -Name f }
# this should be function. It should not be alias, because aliases are used for
# mocking
$command.CommandType | Verify-Equal "Function"
# $command.DisplayName
}
}
b "parameter filter conflicting arguments" {
t "Should -Invoke parameter filter should not use 'arguments' name internally to avoid conflict" {
# https://github.com/pester/Pester/issues/1819
$sb = {
Context "a" {
It "b" {
function a ($Arguments) {}
Mock a
a -Arguments @{ Name = "Jakub" }
Should -Invoke a -ParameterFilter { "Jakub" -eq $Arguments.Name }
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
}
b "Mocking function with custom class attribute" {
t "generating parametrized tests from foreach without external id" {
if ($PSVersionTable.PSVersion.Major -le 4) {
return
}
# https://github.com/pester/Pester/issues/1772
# there is using, it needs to be in a separate file so we can skip it on <PS5
$result = Invoke-Pester $PSScriptRoot/Pester.Mock.ClassMetadata.ps1 -PassThru
$result.Containers[0].Blocks[0].ErrorRecord | Verify-Null
$result.Containers[0].Blocks[0].Tests.Count | Verify-Equal 2
$result.Containers[0].Blocks[0].Tests[0].Passed | Verify-True
}
}
b "Mocking across modules" {
t "Mock defined in module is not called when calling from script" {
$sb = {
BeforeAll {
Get-Module m | Remove-Module
New-Module m -ScriptBlock {
function f ($a) { "real in module m" }
} | Import-Module
}
Describe "d" {
It "i" {
Mock f -ModuleName m { "mock in m" }
# called in script, invokes real function
f | Should -Be "real in module m"
Should -Invoke f -ModuleName m -Exactly 0
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
t "Mock defined in module is called when calling from that module" {
$sb = {
BeforeAll {
Get-Module m | Remove-Module
New-Module m -ScriptBlock {
function f ($a) { "real in module m" }
} | Import-Module
}
Describe "d" {
It "i" {
Mock f -ModuleName m { "mock in module m" }
# called in script, invokes real function
& (Get-Module m ) { f } | Should -Be "mock in module m"
Should -Invoke f -ModuleName m -Exactly 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
t "Mock defined in module is called when public function invokes the mocked function in that module" {
$sb = {
BeforeAll {
Get-Module m, n | Remove-Module
New-Module m -ScriptBlock {
function f ($a) { "real in module m" }
} | Import-Module
New-Module n -ScriptBlock {
# n can call f that is exported from m, but it calls it from within
# module n, so the mock needs to be effective in n
function g () { f }
} | Import-Module
}
Describe "d" {
It "i" {
Mock f -ModuleName n { "mock in module n" }
# this mock in m is ignored
Mock f -ModuleName m { "mock in module m" }
# this mock in script is ignored
Mock f { "mock in script" }
# called in script, but invokes function in module n
g | Should -Be "mock in module n"
Should -Invoke f -ModuleName n -Exactly 1
Should -Invoke f -ModuleName m -Exactly 0
Should -Invoke f -Exactly 0
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
t "Mock defined in module falls back to script default behavior when no default behavior is defined in the module" {
$sb = {
BeforeAll {
Get-Module m | Remove-Module
New-Module m -ScriptBlock {
function f ($a) { "real in module m" }
function g () { f }
} | Import-Module
}
Describe "d" {
It "i" {
# we fallback to this from the module when no filtered mock is matched there
# but we don't fallback to it when we don't have any mock in the module, because
# there is no hook
Mock f { "mock in script" }
Mock f -ModuleName m { "mock in module" } -ParameterFilter { $false }
g | Should -Be "mock in script"
Should -Invoke f -Exactly 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
})
$t = $r.Containers[0].Blocks[0].Tests[0]
$t.Result | Verify-Equal "Passed"
}
}
b "Mock cleanup" {
t "Invoke-Pester cleans up orphaned mock hooks and aliases in modules" {
# define mock like functions in third party module
Get-Module m | Remove-Module
New-Module m -ScriptBlock {
function PesterMock_aaa () { }
Set-Alias -Name aaa -Value PesterMock_aaa
Export-ModuleMember -Function ""
} | Import-Module
# in caller scope
function PesterMock_bbb () { }
Set-Alias -Name bbb -Value PesterMock_bbb
# and in Pester module
. (Get-Module Pester) {
function PesterMock_ccc () { }
Set-Alias -Name aaa -Value PesterMock_aaa
}
# just trigger empty run to get cleanup
Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = {}; PassThru = $true }
})
$moduleCommands = & (Get-Module m) { Get-Command | Where-Object { $_.Name -like "*aaa" } }
$moduleCommands | Verify-Null
$commands = Get-Command | Where-Object { $_.Name -like "*bbb" }
$commands | Verify-Null
$pesterCommands = & (Get-Module Pester) { Get-Command | Where-Object { $_.Name -like "*ccc" } }
$pesterCommands | Verify-Null
}
}