Skip to content

ADDING mocking script for pesterV5 DOCS#370

Open
CN-CODEGOD wants to merge 4 commits into
pester:mainfrom
CN-CODEGOD:main
Open

ADDING mocking script for pesterV5 DOCS#370
CN-CODEGOD wants to merge 4 commits into
pester:mainfrom
CN-CODEGOD:main

Conversation

@CN-CODEGOD
Copy link
Copy Markdown

i read about the issue

And test the function,it work fine in pesterV5 but fail in V4,it is a good assertion ,hoping to add the in scriptscope in modules future

### Mocking function in scriptblock
Unfortunately, we don't have a straightforward way to mock a function inside a scriptblock, but we could use some workarounds to accomplish this.

warning:Mocking may not work correctly in Pester versions below v5, so this function is only supported in Pester v5. In earlier versions, you cannot run the mock together with the script.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not feel right, I wrote about how to do this long time ago, and I don't think there is anything in Pester 5 that would make this work, it is simple mocking.

https://jakubjares.com/2019/06/09/testing-whole-scripts/ https://jakubjares.com/2019/06/14/testing-whole-scripts-part2/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any problem with the script tester ?
As far as i concern the inscript work well

   Add-Type -TypeDefinition @'
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Reflection;

namespace ScriptMocking
{
    [Cmdlet(VerbsData.Import, "Script")]
    public sealed class ImportScriptCommand : PSCmdlet
    {
        private static string Body = @"
            function ____placeholder {{ }}
            # alias to prevent main from running
            Set-Alias {0} '____placeholder'

            . {1} {2}
            # remove the alias, now we have all functions
            # from the script dot-sourced in this scope
            Remove-Item 'alias:{0}' -Force
            Remove-Item 'function:____placeholder' -Force";

        [Parameter(Position = 0, Mandatory = true)]
        public string Path { get; set; }

        [Parameter(Position = 1)]
        public string EntryPoint { get; set; }

        [Parameter]
        public string[] ArgumentList { get; set; }

        public ImportScriptCommand() {
            ArgumentList = new string[0];
            EntryPoint = "Main";
        }
        protected override void ProcessRecord()
        {
            var body =  string.Format(Body, EntryPoint, Path, string.Join(" ", ArgumentList));

            var sb = InvokeCommand.NewScriptBlock(body);

            var method = sb.GetType()
                .GetMethod("InvokeUsingCmdlet", BindingFlags.Instance | BindingFlags.NonPublic);

            var emptyArray = new object[0];
            var automationNull = new PSObject();
            const int writeToCurrentErrorPipe = 1;

            var @params = new object[]
                {this, false, writeToCurrentErrorPipe, automationNull, emptyArray, automationNull, new object[0]};

            method.Invoke(sb, @params);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants