Skip to content

-beOfType doesn't use the instance inside the script #2701

@CN-CODEGOD

Description

@CN-CODEGOD

Checklist

What is the issue?


#test.class.ps1
  
Describe "test the test.class.ps1"{

      BeforeAll {

        #load class.ps1
       # . $psscriptroot/class.ps1
        #load test.class.ps1
        . "O:\OneDrive\.modules\import-xml\test.class.ps1"
    }
    beforeEach {
        

        $nestedPscustomobject = [PSCustomObject]@{
            testInstance = [PSCustomObject]@{
                name = "default name"
                text = "default text"
            }
            text = "text"
        }


        $hashtablePsCustomObject=@([PSCustomObject]@{
    hash = @{key1 = "value1"; key2 = "value2"}
    text = "custom text"
            })

            $arrayPsCustomObject=@(
        [PSCustomObject]@{
            array = @(1, 2, 3)
            text = "text"
        }
    )

    $scriptblockPsCustomObject=@([PSCustomObject]@{
                scriptblock = {Write-Host "Hello, World!"}
                text = "text"})

    }
context "nestedInstanceTest" {
    
    it 'Test the Pscustomobject Doinit method'{
        #$object =new-object -typename "nestedInstance" -ArgumentList $nestedPscustomobject
        $nestedPscustomobject|should -not -BeNullOrEmpty
        $object=[nestedInstance]::new($nestedPscustomobject)
       # $object |should -beoftype "nestedInstance" 
        $object.text|should -be "text"
        $object.testInstance.name|should -be "default name"
        $object.testInstance.text|should -be 'default text'
    }

}
Context "hashtableInstanceTest" -skip{

    it 'test the void constructor' -skip{

    }

    it 'test the Pscustomobject doinit method'{


    }


}

Context "scriptblockInstancTest" -skip {

    it 'test the void constructor'{


    }

    it 'test the Pscustomobject doinit method'{


    }
}

Context "arrayInstanceTest" -skip{

    it 'test the void Contructor'{

    }
    it 'test the Pscustomobject doinit method'{


    }
}

}

I wrote a script testing the doinit method and Instance
but was given error
[-] test the test.class.ps1.nestedInstanceTest.Test the Pscustomobject Doinit method 15ms (14ms|2ms)

ArgumentException: Could not find type [nestedInstance]. Make sure that the assembly that contains that type is loaded.

i though it was the instance creation went wrong ,but after some debug it was the -beOftype given such error
the -beOfType command doesn't work when the instance was load in the powershell .

i wrote a test file to implement the issue

Describe "Debug why class.ps1 dosen's load correctly"{

    BeforeAll {

        #load class.ps1
        . $psscriptroot/class.ps1
  
    }

    
    it 'test fail' {

       $testInstance = [testinstance]::new()

       $testInstance|should -not -BeNullOrEmpty
    
       $testInstance|should -beoftype testInstance

       #should fail
    }

    it 'test success'{

       $testInstance = [testinstance]::new()

       $testInstance|should -not -BeNullOrEmpty
    
      #$testInstance|should -beoftype testInstance

       #should passed

    }
}
#class.ps1

class testInstance  {
    [string]$text
    [string]$name
   



}

Expected Behavior

pass the test

Steps To Reproduce

null

Describe your environment

PS O:\OneDrive.modules\import-xml> get-module pester

ModuleType Version PreRelease Name ExportedCommands


Script 5.7.1 Pester {Add-ShouldOperator, AfterAll, AfterEach, Assert-MockCalled…}

PS O:\OneDrive.modules\import-xml>

Possible Solution?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions