Stephanevg / PSClassUtils

A set of utilities to work with Powershell Classes
http://powershelldistrict.com/how-to-generate-a-uml-diagram-using-powershell/
91 stars 23 forks source link

Generated Tests assumes that the a parameterless constructor is avaiable. #121

Open Stephanevg opened 4 years ago

Stephanevg commented 4 years ago

This is not always the case. See example below.


Class PCCPackage {
    [String]$Name
    [version]$Version
    [system.io.DirectoryInfo]$Path
    [String]$OS
    [String]$Type
    [String]$Platform

    PCCPackage([String]$Name,[String]$Path){
        $this.Name = $Name
        $this.Path = $Path
        $This.SetValues()

    }

    SetValues(){
        $this._SetVersion()
        $this._SetPlatform()
        $this._SetOS()
        $this._SetType()
    }

    Hidden _SetVersion(){
        try{

            $ver = (Get-Module $this.Path -ListAvailable -ErrorAction Stop).Version.ToString() 
        }Catch{
            $ver = $null
        }
        $this.Version = $ver
    }

    hidden _SetPlatform(){
        $p =  $this.Name.Split(".")[2]
        $This.Platform =$p
    }

    hidden _SetOS(){

        $this.OS = $this.Name.Split(".")[3]
    }

    Hidden _SetType(){
        $t = $this.Name.Split(".")[4]
        $This.Type = $t
    }

    Hidden [String] GetOS(){
        return $this.Os

    }

    [String] GetPath(){
        REturn $this.Path.Full
    }
    [version] GetVersion(){
        return $this.version
    }
}

generates

Describing [PCCPackage]-[Constructors]
    [-] [PCCPackage]-[Constructor]([String]Name,[String]Path) should Not Throw 98ms
      Expected no exception to be thrown, but an exception "Exception setting "Path": "Cannot convert value "" to type "System.IO.DirectoryInfo". Error: "The path is not of a legal form.""" was thrown from C:\Users\taavast3\OneDrive\Repo\Projects\Clients\ee\ESC\PCCComplianceChecks\Code\Classes\Class.PCCPackage.ps1:11 char:9
          +         $this.Path = $Path
          +         ~~~~~~~~~~~~~~~~~~.
      18: {[PCCPackage]::New($Name,$Path)} | Should Not Throw
      at <ScriptBlock>, C:\Users\taavast3\OneDrive\Repo\Projects\Clients\hh\ESC\PCCComplianceChecks\Code\Classes\Class.PCCPackage.Tests.Ps1: line 18

  Describing [PCCPackage]-[Methods]
    [-] [PCCPackage] --> SetValues() :  - should Not Throw 28ms
      MethodException: Cannot find an overload for "new" and the argument count: "0".
      at <ScriptBlock>, C:\Users\taavast3\OneDrive\Repo\Projects\Clients\hh\ESC\PCCComplianceChecks\Code\Classes\Class.PCCPackage.Tests.Ps1: line 33
    [-] [PCCPackage] --> SetValues() :  - should return type [] 30ms
      MethodException: Cannot find an overload for "new" and the argument count: "0".
      at <ScriptBlock>, C:\Users\taavast3\OneDrive\Repo\Projects\Clients\hh\ESC\PCCComplianceChecks\Code\Classes\Class.PCCPackage.Tests.Ps1: line 48
    [-] [PCCPackage] --> _SetVersion() :  - should Not Throw 32ms

Here there is no parameterless constructor (I'll add in my production code) but Write-CUPesterTEsts should assume that a parameterless constructor is present.

If no parameterless constructor is present, is should use the first one of the list available (see Get-CUConstructor)