Should Be assertion fails on newline character

38 views
Skip to first unread message

cra...@gmail.com

unread,
Jun 9, 2015, 3:52:21 PM6/9/15
to pes...@googlegroups.com
I'm having an issue comparing a file's content to an expected value.

Describe "Invoke-CsvCleanser" {

    $nl
= [Environment]::NewLine
    $content
= 'DATE_COLUMN,DATETIME_COLUMN,TEXT_COLUMN' + $nl + '2015-05-01,2015-05-01 23:00:00.000,LOREM IPSUM' + $nl + 'NULL,NULL,NULL'

   
# create CSV file; populate it with data
    $csv
= New-item "TestDrive:\000.csv" -Type File
   
Set-Content $csv -Value $content

   
It "Should remove all instances of the word 'NULL'" {

       
# remove all instances of 'NULL' from CSV file
       
Invoke-CsvCleanser $csv -Nulls

        $actual
= Get-Content $csv
        $expected
= 'DATE_COLUMN,DATETIME_COLUMN,TEXT_COLUMN' + $nl + '2015-05-01,2015-05-01 23:00:00.000,LOREM IPSUM' + $nl + ',,'

        $actual
| Should Be $expected
   
}

}

Unfortunately, the assertion fails with the message:

 [-] Should remove all instances of the word 'NULL' 63ms
   
Expected string length 91 but was 39. Strings differ at index 39.
   
Expected: {DATE_COLUMN,DATETIME_COLUMN,TEXT_COLUMN\r\n2015-05-01,2015-05-01 23:00:00.000,LOREM IPSUM\r\n,,}
   
But was:  {DATE_COLUMN,DATETIME_COLUMN,TEXT_COLUMN}
   
--------------------------------------------------^
   at line
: 23 in C:\Users\xxx\Documents\WindowsPowerShell\Scripts\Invoke-CsvCleanser\Invoke-CsvCleanser.Tests.ps1
   
23:         $actual | Should Be $expected

What am I missing/not understanding?

Dave Wyatt

unread,
Jun 9, 2015, 3:54:43 PM6/9/15
to pes...@googlegroups.com
Get-Content is returning an array of strings, rather than a single string that contains multiple lines.  When you pipe an array to Should, the assertion is run against each element of the array.  In this case, you'd probably want something like this:

$actual = (Get-Content $csv) -join $nl

cra...@gmail.com

unread,
Jun 9, 2015, 4:01:35 PM6/9/15
to pes...@googlegroups.com
That worked.  Thanks.
Reply all
Reply to author
Forward
0 new messages