Mock Environment Variable

911 views
Skip to first unread message

Dave Wiard

unread,
Oct 6, 2016, 10:58:35 AM10/6/16
to Pester
We have a function that does nothing more than check the value of [System.Environment]::OSVersion.Version for specific values and returns a $true/$false result. I saw a post on Stack Overflow that demonstrated how to do this but it does not appear to work for all variables. When I tried to set the OSVersion value within a Pester test I received the error below.

    [-] Error occurred in Context block 327ms
     
SetValueException: 'OSVersion' is a ReadOnly property.
     
RuntimeException: 'OSVersion' is a ReadOnly property.

Is there a way to mock these system environment variables or is this a function that simply cannot be tested using mocked values?

Dave Wyatt

unread,
Oct 6, 2016, 3:28:46 PM10/6/16
to Dave Wiard, Pester
You can write a function that just returns the value of an environment variable (which doesn't really much testing), then mock that.

--
You received this message because you are subscribed to the Google Groups "Pester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pester+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave Wiard

unread,
Oct 12, 2016, 6:06:19 PM10/12/16
to Pester
Thanks, Dave. This is probably what we'll end up doing. For now I've moved on to other, larger, issues with our current Pester suite.
Reply all
Reply to author
Forward
0 new messages