大家好我已经编写了一个脚本来显示当前的用户信息,我想编写Pester测试用例,应该模拟输出,如果我在函数中没有return
我该怎么写对此的测试
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'
}
}
哪种方式适用于我的电脑但是当我在其他电脑上执行相同操作时它会失败,所以我怎样才能使它独一无二p>
答案 0 :(得分: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
}