Comments (7)
This would likely be better served by LocalGroupMembership resource and maybe a LocalUser resource since the Win32_Group class does not have a members property and the Win32_UserAccount does not have a memberof property. Also in Pester, the "Contain" assertion does not work with arrays, however "Be" does work.
Juan
from poshspec.
@devblackops The goal was to get a lot of the logic outside of the scope of the Pester functions to enable testing. The parsing of the scriptblock replaces the variables with their value - sort of half-evaluating the expression. There's probably a better way to do it. You could move that snippet into a new Private function like Get-LocalGroupMembers
that you call from LocalGroup
.
That is a good example. I'll see about making it supported without a function.
from poshspec.
@cdhunt Thanks. I'll mess around with extending LocalGroup
or creating a new test for group membership.
from poshspec.
The other downside to parsing $TestExpression as a string is that when you have a variable in your expression it isn't interpolated in the printed output.
EDIT: I've quickly and hackily worked around this like so:
function Fix-PoshSpecInterpolation{
param($CodeBlock)
$matches = @();
$CodeString = $CodeBlock.ToString().Trim()
if($CodeString -match '\$[\w:]*'){
foreach($variable in $matches.Values){
$CodeString = $CodeString.replace($variable, "`"$(iex $variable)`"")
}
}
return [scriptblock]::Create($CodeString)
}
Describe 'Website Host Entry' {
File "$Env:SYSTEMROOT\system32\drivers\etc\hosts" $(Fix-PoshSpecInterpolation{ Should Contain $SiteName})
}
Which is ugly as all hell and won't deal with quotes gracefully, but someone may find it useful.
from poshspec.
I assumed the value in the Should block would be a constant, but it should be possible to expand variables with $ExecutionContext.InvokeCommand.ExpandString()
the same way we do on the other part of the expression.
from poshspec.
@cdhunt Hah, amazing, I didn't realise that's what that was doing.... So much cleaner now.
function Fix-PoshSpecInterpolation{
param($CodeBlock)
$CodeString = $CodeBlock.ToString().Trim()
return [scriptblock]::Create($ExecutionContext.InvokeCommand.ExpandString($CodeString))
}
Describe 'Website Host Entry' {
File "$Env:SYSTEMROOT\system32\drivers\etc\hosts" $(Fix-PoshSpecInterpolation{ Should Contain $SiteName})
}
Thank you! Would you be up for me submitting a PR with this fixed in the module?
from poshspec.
I just tested adding $assertion = $ExecutionContext.InvokeCommand.ExpandString($assertion)
to Get-PoshSpecParam and it works with one caveat, the variable must be defined in Global scope. The way you solved it works really well, but does add a fair amount of extra "stuff" to the line.
I shortened it with an alias:
Set-Alias -Name exp -Value Fix-PoshSpecInterpolation
Describe "test should block" {
$running = "Running"
Service w32time Status (Exp{Should Be $running})
}
Which is easier on the user?
from poshspec.
Related Issues (20)
- Unable to call Custom functions in poshspec.
- Http tests don't work against https URLs with invalid certs HOT 4
- Additional Test: AuditPolicy
- Additional Test: LocalUser
- Additional Test: UserRightsAssignment
- Additional Private Function: Test-RunAsAdmin
- Service Function: Get-Service vs Get-WmiObject\Get-CimInstance HOT 1
- Pester 4.x support - Allow for new syntax HOT 2
- Documentation Transition HOT 3
- CHANGELOG HOT 1
- Poshspec Pester Tests broken from PR #44 HOT 1
- more efficient method for package checking HOT 1
- Path prompt with SoftwareProduct HOT 9
- How to use "File Should Contain" rule HOT 4
- PowerShell Gallery shows Pester functions in this module HOT 1
- Update Wiki HOT 2
- Unable to test PowerShell Auditing Settings via registry HOT 2
- Cannot bind argument to parameter 'Command' because it is null. HOT 1
- Compatibility with latest versions of pester
- Issues
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from poshspec.