Functions/Convert-CsvStringToExcelWorkbook.Tests.ps1
describe "BitTitan.Modules.Csv/Convert-CsvStringToExcelWorkbook" -Tag "module", "unit" { # Import ImportExcel for the [OfficeOpenXml.ExcelPackage] type Import-ExternalModule ImportExcel -RequiredVersion 5.3.4 # Import the function to test . "$($PSScriptRoot)\Convert-CsvStringToExcelWorkbook.ps1" # Prepare the input $csvString = @" sep=, "name1","name2","name3" "1 ",2,"-1" True,"False","TRUE " "@ # Declare mocks and external functions mock Test-Path { return $true } function Export-Excel { param ( $TargetData, $Path, $WorksheetName, [Switch]$PassThru ) return New-MockObject -Type OfficeOpenXml.ExcelPackage } context "when there are no issues" { # Declare mocks mock Export-Excel { return New-MockObject -Type OfficeOpenXml.ExcelPackage } it "converts a CSV string to an Excel workbook" { # Call the function $output = Convert-CsvStringToExcelWorkbook -CsvString $csvString -ExcelWorkbookFilePath "C:\temp\excel.xls" ` -ExcelWorksheetName "worksheet1" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Export-Excel -Times 1 -Exactly -ParameterFilter { $Path -eq "C:\temp\excel.xls" -and $WorksheetName -eq "worksheet1" } -Scope it # Verify the output $errorVariable | Should BeNullOrEmpty $output | Should Not Be $null } } context "when the destination directory for the Excel workbook is not present" { # Declare mocks mock Test-Path { return $false } mock Export-Excel { return New-MockObject -Type OfficeOpenXml.ExcelPackage } it "outputs an error" { # Call the function $output = Convert-CsvStringToExcelWorkbook -CsvString $csvString -ExcelWorkbookFilePath "C:\temp\excel.xls" ` -ExcelWorksheetName "worksheet1" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Export-Excel -Times 0 -Exactly -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $null } } context "when an exception occurs while exporting the data to Excel" { # Declare mocks mock Export-Excel { throw "up" } it "outputs an error" { # Call the function $output = Convert-CsvStringToExcelWorkbook -CsvString $csvString -ExcelWorkbookFilePath "C:\temp\excel.xls" ` -ExcelWorksheetName "worksheet1" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Export-Excel -Times 1 -Exactly -ParameterFilter { $Path -eq "C:\temp\excel.xls" -and $WorksheetName -eq "worksheet1" } -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $null } } context "when the export of the data to Excel fails" { # Declare mocks mock Export-Excel {} it "outputs an error" { # Call the function $output = Convert-CsvStringToExcelWorkbook -CsvString $csvString -ExcelWorkbookFilePath "C:\temp\excel.xls" ` -ExcelWorksheetName "worksheet1" -ErrorAction SilentlyContinue -ErrorVariable errorVariable # Verify the mocks Assert-MockCalled Export-Excel -Times 1 -Exactly -ParameterFilter { $Path -eq "C:\temp\excel.xls" -and $WorksheetName -eq "worksheet1" } -Scope it # Verify the output $errorVariable | Should Not BeNullOrEmpty $output | Should Be $null } } } |