A parameter cannot be found that matches parameter name

1,027 views
Skip to first unread message

Ben Flowers

unread,
Nov 27, 2013, 5:01:33 AM11/27/13
to pes...@googlegroups.com
Hello,

I am trying to mock the New-Item function in the WebAdministration module. This seems to be an overridden version of the New-Item method which gives it new parameters.

I Need to pass in physicalPath for creating a website, however when i do i get the followign failure:

[-]   Should Create A New Site when the Site does not exist 93ms
   A parameter cannot be found that matches parameter name 'physicalPath'.
   at line: 13 in C:\_src\AppCmdRuby\Test\Site\Create-Site.NewSite.Tests.ps1

Here is my test code:

 It "Should Create A New Site when the Site does not exist" {
        $name = "HelloWorld"
        $script:created_path = $null
        Mock New-Item -MockWith {
            param($physicalPath)
            Set-Variable -Name created_path -Value $Path -Scope script
        }  

        Mock Get-Item  -MockWith {
            return $null
        }

        Create-Site $name
        $created_path | Should Be "IIS:\Sites\$name"
    }



If anyone could help me, that would be massively appreciated!!

Thanks,

Ben

Scott Muc

unread,
Dec 2, 2013, 2:39:20 AM12/2/13
to pes...@googlegroups.com
Hi Ben,

I think seeing Create-Site would be helpful here. Does the test work if you remove Create-Site and use the mocked New-Item inside the test?

Scott

Ben Flowers

unread,
Dec 2, 2013, 12:15:28 PM12/2/13
to pes...@googlegroups.com
Hi Scott, 


Thanks for getting back to me.  The code has changed from when I posted this. I Have now managed to get it working, by changing my tests format so that all the actions occur in the describe rather than inside the Its:

$current_directory = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$current_directory\..\..\lib\Create-Site.ps1"
Describe "Create-Site New-item" {
    $script:site_name = $null
    $script:applicationPool = $null
    $script:forced = $false
    $website_name = "HelloWorld"
    $app_pool = "HelloWorld"
    $bindings_string = "http:*:80:helloworld.authoring"

    Mock Get-Item  -MockWith {
        return $null
    }
    Mock Set-ItemProperty {}
    Mock New-Website -MockWith {
        Set-Variable -Name site_name -Value $Name -Scope script
        Set-Variable -Name applicationPool -Value $ApplicationPool -Scope script
        Set-Variable -Name forced -Value $force -Scope script
    } 
    Create-Site $website_name $app_pool $app_path $bindings_string

    It "Should Create A New Site when the Site does not exist" {        
        $site_name | Should Be "$website_name"
    }
    It "Should Create A New Site with the correct application pool" {
        $applicationPool | Should Be "$app_pool"
    }
    It "Should Force the Website Creation without a physical path" {
        $forced | Should Be $true
    }
}


The way i was doing it before would have been something like this (this is just me writing it in the groups so ignore syntax errors :)

$current_directory = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$current_directory\..\..\lib\Create-Site.ps1"
Describe "Create-Site New-item" {
    $website_name = "HelloWorld"
    $app_pool = "HelloWorld"
    $bindings_string = "http:*:80:helloworld.authoring"

    
    Mock Get-Item  -MockWith {
        return $null
    }
    Mock Set-ItemProperty {}

    It "Should Create A New Site when the Site does not exist" {   
        $script:site_name = $null
        Mock New-Website -MockWith {
            Set-Variable -Name site_name -Value $Name -Scope script
        }
        Create-Site $website_name $app_pool $app_path $bindings_string
        $site_name | Should Be "$website_name"
    }
    It "Should Create A New Site with the correct application pool" {
        $script:applicationPool = $null
        Mock New-Website -MockWith {
            Set-Variable -Name applicationPool -Value $ApplicationPool -Scope script
        }
        Create-Site $website_name $app_pool $app_path $bindings_string
        $applicationPool | Should Be "$app_pool"
    }
    It "Should Force the Website Creation without a physical path" {
        $script:forced = $false
        Mock New-Website -MockWith {
             Set-Variable -Name forced -Value $force -Scope script
        }
        Create-Site $website_name $app_pool $app_path $bindings_string
        $forced | Should Be $true
    }
}

This way seems to work find for up to two Its, however on the 3rd It falls over and seems to be using the wrong mock. So I dont know if this is a bug. or i was trying to get the framework to do something it shouldn't?


here is the create site function if you are still interested:
$current_directory =  Split-Path $script:MyInvocation.MyCommand.Path
$binding_builder_path = "$current_directory\Bindings-Builder.ps1"

function Create-Site($name,$applicationPool,$physicalPath, $bindings_string){
    Import-Module WebAdministration
    $site_path = "IIS:\Sites\$name"
    $bindings_builder = Import-Module $binding_builder_path
    $bindings = $bindings_builder.build($bindings_string)

    $site = Get-Item $site_path -ErrorAction SilentlyContinue
    if(!$site){
        New-Website $name -applicationPool $applicationPool -force
    } 
    Set-ItemProperty $site_path -name bindings -value $bindings
    if($physicalPath){
      Set-ItemProperty $site_path -name physicalPath -value $physicalPath  
    }
}
 


Cheers,

Ben
Reply all
Reply to author
Forward
0 new messages