2016-08-16 67 views
0

大家好我已經寫了一個腳本來顯示當前的用戶信息,我想寫Pester測試用例應該模擬輸出,如果我沒有return函數如何我可以寫一個測試過Pester嘲笑輸出變化的腳本

function Get-CurrentUserInfo 
{ 

    $domain = [Environment]::UserDomainName 
    $user = [Environment]::UserName 
    if (!([string]::IsNullOrEmpty($domain))) { $domain = $domain + '\' } 

    $currentUser = $domain + $user 

    #return $currentUser I have commented out so that it will not return any output 
} 

這裏是我的測試案例時有返回

$here = Split-Path -Parent $MyInvocation.MyCommand.Path 
. "$here\Get-CurrentUserInfo.ps1" 
Describe "CurrentUser" { 
    It "CurrentUser Info" { 
     Get-CurrentUserInfo | Should be 'MY-PC\username' 
    } 
} 

的正常工作與我的電腦,但是當我在其他PC執行相同的它會失敗,因此我怎樣才能讓它獨一無二

回答

1

你可以嘲笑ENV變量:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path 
. "$here\Get-CurrentUserInfo.ps1" 
Describe "CurrentUser" { 
    $originaldomain = [Environment]::UserDomainName 
    $originaluser = [Environment]::UserName 

    [Environment]::UserDomainName = "testdomain" 
    [Environment]::UserName = "testuser" 


    It "CurrentUser Info" { 
     Get-CurrentUserInfo | Should be 'testdomain\testuser' 
    } 

    [Environment]::UserDomainName = $originaldomain 
    [Environment]::UserName = $originaluser 
} 
+0

我試圖用你的'$ ENV答案:systemdrive'。在'beforeAll'部分設置它時,看起來不起作用。你有更多的信息嗎? – restless1987