Fix GetType() bad pattern and related issues in tests (#3134)

* Fix GetType() bad pattern and related issues in tests

$var.GetType() can raise an exception in tests so we should check $var
before make the call. A large part of the tests does not make this
check.
I start with searching ".GetType()" but discovered many related issues
in tests (reduntant and unneeded tests, "throw" bad pattens, bugs,
formattings (sorry!) and so on) - I had to fix them too.

* Fix after code review
* Second wave of migration GetType() -> BeOfType
Removed 'GetType().Name' patterns.
This commit is contained in:
Ilya
2017-02-15 16:40:51 -08:00
committed by Dongbo Wang
parent d801b75d35
commit 409ab7443f
62 changed files with 381 additions and 443 deletions
@@ -19,9 +19,9 @@
It '$err.Count' { $err.Count | Should Be 1 }
It '$err[0] should not be $null' { $err[0] | Should Not Be $null }
It '$err[0].GetType().Name' { $err[0].GetType().Name | Should Be ActionPreferenceStopException }
It '$err[0].GetType().Name' { $err[0] | Should BeOfType "System.Management.Automation.ActionPreferenceStopException" }
It '$err[0].ErrorRecord' { $err[0].ErrorRecord | Should not BeNullOrEmpty }
It '$err[0].ErrorRecord.Exception.GetType().Name' { $err[0].ErrorRecord.Exception.GetType().Name | Should Be ItemNotFoundException }
It '$err[0].ErrorRecord.Exception.GetType().Name' { $err[0].ErrorRecord.Exception | Should BeOfType "System.Management.Automation.ItemNotFoundException" }
}
It 'ActionPreference Ignore Works' {
@@ -290,17 +290,13 @@ Describe "Unit tests for various script breakpoints" -Tags "CI" {
{
& $command
It "Script should fail to verify exception" {
$false | Should Be $true
}
throw "No Exception!"
}
catch
{
$type = $_.Exception.GetType().Name
It "Script failed expected exception" {
$type | Should Be $exception
}
It "Script failed expected exception" {
$_.Exception.GetType().Name | Should Be $exception
}
}
}
@@ -52,7 +52,8 @@ Describe 'Testing of script internationalization' -Tags "CI" {
import-localizedData mydata -uiculture nl-NL -ea SilentlyContinue -ev ev
$ev[0].Exception.GetType() | Should Be System.Management.Automation.PSInvalidOperationException
$ev | Should Not BeNullOrEmpty
$ev[0].Exception | Should BeOfType "System.Management.Automation.PSInvalidOperationException"
}
It 'Import different file name is done correctly' {
@@ -110,7 +111,8 @@ Describe 'Testing of script internationalization' -Tags "CI" {
import-localizedData mydata -filename bad
}
$script:exception.exception.gettype() | Should Be System.management.automation.psinvalidoperationexception
$script:exception.exception | Should Not BeNullOrEmpty
$script:exception.exception | Should BeOfType System.management.automation.psinvalidoperationexception
}
It 'Import if psd1 file is done correctly' {
@@ -8,9 +8,10 @@ else {
$PesterSkipOrPending = @{}
}
Describe "NativeLinuxCommands" -tags "CI" {
It "Should return a type of System.Object for hostname cmdlet" {
(hostname).GetType().BaseType | Should Be 'System.Object'
(hostname).GetType().Name | Should Be String
It "Should return a type of 'string' for hostname cmdlet" {
$result = hostname
$result | Should Not BeNullOrEmpty
$result | Should BeOfType string
}
It "Should find Application grep" @PesterSkipOrPending {
@@ -290,12 +290,19 @@ Describe "Update both OutVariable and ErrorVariable" -Tags "CI" {
Context 'Error variable in multi-command pipeline (with native cmdlet)' {
(get-foo -ev foo_err | get-item -ev get_item_err ) 2>&1 > $null
BeforeAll {
(get-foo -ev foo_err | get-item -ev get_item_err ) 2>&1 > $null
}
$foo_err | Should Be "foo-error"
It '$foo_err should be "foo-error"' {
$foo_err | Should Be "foo-error"
}
It '$get_item_err.count' { $get_item_err.count | Should Be 1 }
It '$get_item_err[0].exception' { $get_item_err[0].exception.GetType() | Should Be 'System.Management.Automation.ItemNotFoundException' }
It '$get_item_err.count and $get_item_err[0].exception' {
$get_item_err.count | Should Be 1
$get_item_err[0].exception | Should Not BeNullOrEmpty
$get_item_err[0].exception | Should BeOftype 'System.Management.Automation.ItemNotFoundException'
}
}
It 'Multi-command pipeline with nested commands' {