Functions/Import-ExternalModule.Tests.ps1
describe "BitTitan.Runbooks.Modules/Import-ExternalModule" -Tags "module", "unit" { # Import the function to test . "$($PSScriptRoot)\Import-ExternalModule.ps1" # Declare Get-Module to override the existing function # If we don't do this the mock won't work function Get-Module { param ($Name, [Switch]$ListAvailable) } # Declare Install-Module to override the existing function # If we don't do this the mock won't work function Install-Module { param ($Name, $RequiredVersion) } context "when no version is not currently installed" { # Mock Get-Module mock Get-Module {} # Mock Install-Module mock Install-Module {} # Mock Import-Module mock Import-Module {} it "installs the required version of the module and imports it" { # Call the function Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.5 -Quiet -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $ListAvailable -and $Name -eq "MyExternalModule" } Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq "1.2.3.5" } Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq "1.2.3.5" } # Verify the output $errorVariable | Should BeNullOrEmpty } # Mock Get-Module mock Get-Module {} it "installs the module and imports it" { # Call the function Import-ExternalModule MyExternalModule -Quiet -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $ListAvailable -and $Name -eq "MyExternalModule" } -Scope it Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq $null } -Scope it Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq $null } -Scope it # Verify the output $errorVariable | Should BeNullOrEmpty } } context "when the required version is not currently installed" { # Mock Get-Module mock Get-Module { return [PSCustomObject]@{ Version = "1.2.3.4" } } # Mock Install-Module mock Install-Module {} # Mock Import-Module mock Import-Module {} it "installs the required version of the module and imports it" { # Call the function Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.5 -Quiet -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $ListAvailable -and $Name -eq "MyExternalModule" } Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq "1.2.3.5" } Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq "1.2.3.5" } # Verify the output $errorVariable | Should BeNullOrEmpty } # Mock Get-Module mock Get-Module {} it "installs the module and imports it" { # Call the function Import-ExternalModule MyExternalModule -Quiet -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $ListAvailable -and $Name -eq "MyExternalModule" } -Scope it Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq $null } -Scope it Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq $null } -Scope it # Verify the output $errorVariable | Should BeNullOrEmpty } } context "when the required version is currently installed" { # Mock Get-Module mock Get-Module { return [PSCustomObject]@{ Version = "1.2.3.4" } } # Mock Install-Module mock Install-Module {} # Mock Import-Module mock Import-Module {} it "imports the required version of the module" { # Call the function Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.4 -Quiet -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $ListAvailable -and $Name -eq "MyExternalModule" } -Scope it Assert-MockCalled Install-Module -Times 0 -Exactly -Scope it Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq "1.2.3.4" } -Scope it # Verify the output $errorVariable | Should BeNullOrEmpty } it "imports the module" { # Call the function Import-ExternalModule MyExternalModule -Quiet -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $ListAvailable -and $Name -eq "MyExternalModule" } -Scope it Assert-MockCalled Install-Module -Times 0 -Exactly -Scope it Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "MyExternalModule" -and $RequiredVersion -eq $null } -Scope it # Verify the output $errorVariable | Should BeNullOrEmpty } } # Declare the functions to throw an exception $functionsToThrowExceptions = @( "Get-Module", "Install-Module", "Import-Module" ) foreach ($function in $functionsToThrowExceptions) { context "when $($function) throws an exception" { # Mock Get-Module mock Get-Module { return [PSCustomObject]@{ Version = "1.2.3.4" } } # Mock Install-Module mock Install-Module {} # Mock Import-Module mock Import-Module {} # Mock the function to throw an exception mock $function { throw "throws exception" } it "fails to install/import the module and outputs an error" { # Call the function Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.5 -Quiet -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the output $errorVariable | Should Not BeNullOrEmpty } } } } |