Pester Test doesn't like code that works in script when run manually

92 views
Skip to first unread message

Matt Short

unread,
May 31, 2016, 7:07:52 AM5/31/16
to Pester
Wonder if I could get a little help with the following error?

the code.
# Merge arrays adding a zero so we allways start issuing numbers from the beginning (ie 001)
$list
= New-Object System.Collections.ArrayList
$list
+= $VMNumbers + $ADnumbers + $DNSNumbers + $SCCM2012Numbers + $SCCM2007Numbers + $SCOM2007Numbers

the error

[-] returns a server name 95.02s
   RuntimeException: Method invocation failed because [System.Object[]] does not contain a method named 'op_Addition'.
   at <ScriptBlock>, C:\Powershell\Get-NextServerName\Get-NextServerName.ps1: line 163
   at <ScriptBlock>, C:\Powershell\Get-NextServerName\Get-NextServerName.Tests.ps1: line 7

The line erroring is the bottom line in the code block.
is it fair to summise that the Pester code doesn't have access to the System.Collections.ArrayList object?

I'm trying to use Pester in advance of showing my colleagues how to do testing in powershell. I've managed to follow the hello world examples, not attempting my first test on code already in use. and i've killed it. lol.
 

Chris Dent

unread,
May 31, 2016, 8:12:22 AM5/31/16
to Matt Short, Pester
You should be using $list.Add and / or $list.AddRange when you implement an array list. The addition operators work well enough for regular arrays (back-ground implementing an array copy for each addition), provided you ensure each and every one of those things is an array (or can be coerced into being an array). The moment you use op_Addition on ArrayList (in PS) it turns it back into a standard array ($list.GetType() -isnot [System.Collections.ArrayList]).

Using the ArrayList methods will most likely fix your problem here.

Chris

--
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+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Short

unread,
May 31, 2016, 9:25:00 AM5/31/16
to Pester, matts...@gmail.com
awesome response, just done a quick read around what you're saying, rewritten that line and i think i'm doing it right now.

just got to work out why pester seems to run the script twice now. 

Chris Dent

unread,
May 31, 2016, 10:53:03 AM5/31/16
to Matt Short, Pester
Just a quick correction; this proof:

$list.GetType() -isnot [System.Collections.ArrayList]

Should have been either:

$list -isnot [System.Collections.ArrayList]

Or:

$list.GetType() -ne [System.Collections.ArrayList]

Just for clarity. It's a misleading reference otherwise.

Chris

On 31 May 2016 at 14:25, Matt Short <matts...@gmail.com> wrote:
awesome response, just done a quick read around what you're saying, rewritten that line and i think i'm doing it right now.

just got to work out why pester seems to run the script twice now. 

--
Reply all
Reply to author
Forward
0 new messages