. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" #region Add-HostsFileContent Describe "Add-HostsFileContent" { Mock -CommandName Write-Warning {} -ModuleName $moduleForMock Context "When an existing IP address is supplied" { Mock -CommandName Get-HostsFileContent { return @("127.0.0.1 foo.bar", "192.168.1.1 bar.foo") } -ModuleName $moduleForMock It "Writes a Warning" { Add-HostsFileContent "127.0.0.1 hello.world" Assert-MockCalled -CommandName "Write-Warning" -ParameterFilter {$Message -match "hosts entry already exists"} -ModuleName $ModuleToMock } It "Does Not Save the Hosts File" { Add-HostsFileContent "127.0.0.1 hello.world" $hostsFile = Get-HostsFileContent $hostsFile -match "hello.world" | Should -BeNullOrEmpty } } Context "When Valid Parameters are Supplied" { $tempPath = [System.IO.Path]::GetTempFileName() Mock -CommandName Get-HostsFileContent { return @("127.0.0.1 foo.bar", "192.168.1.1 bar.foo") } -ModuleName $moduleForMock It "Adds a Single Host Entry" { Add-HostsFileContent "10.10.10.10 Hello.World" $tempPath $content = Get-Content $tempPath $content -match "Hello.World" | Should -BeTrue } It "Adds Multiple Host Entries" { $mockContent = @("10.10.10.10 Hello.World", "9.9.9.9 No.Way.Jose") Add-HostsFileContent $mockContent $tempPath $content = Get-Content $tempPath $content -match "Hello.World" -and $content -match "No.Way.Jose" | Should -BeTrue } } } #endregion Add-HostsFileContent