
If you’re familiar with classes but aren’t familiar with them in PowerShell, checkout the post PowerShell v5 Classes & Concepts by Michael Willis. I tend to define mock-classes one scope up from where I’ll use them. We did this because the context and it child-blocks have visibility into the parent describe block. Lastly, the classes were defined in a describe block. We do this because you can’t create a variable of type fake_smo_connnection_context until type fake_smo_connnection_context has been defined.

Since fake_smo_server contains fake_smo_connnection_context we’re declaring fake_smo_connnection_context before fake_smo_server. We’re only creating the parts of the server object needed to unit test this function. We’re not creating a complete facsimile of the type we want to mimic. fake_smo_connnection_context mocks the ConnectionContext type. fake_smo_server, a class to mock the server objectįake_smo_connnection_context has single method named Connect() that does nothing and a single property ConnectTimeout.fake_smo_connnection_context, a class to mock the server’s ConnectionContext object.To illustrate the fix we’ll use the following example: function do-thing | Should -Throw As result, the method is executed when you run your test script. NET objects, and you can’t mock those method calls. Unfortunately, several of the functions call methods of. You want to use Pester, a unit testing framework for PowerShell, to create unit tests for those functions. You have written several functions in a PowerShell script or module.
