3

I have a Pester test where I mock up a Read-Host call for my function, which follows the format in this question here:

How do I mock Read-Host in a Pester test?

Describe "Test-Foo" {
    Context "When something" {
    Mock Read-Host {return "c:\example"}

        $result = Test-Foo

        It "Returns correct result" { # should work
            $result | Should Be "c:\example"
        }
         It "Returns correct result" { # should not work
            $result | Should Be "SomeThingWrong"
        }
    }
}

My tests run perfectly when using this format, and calling the test directly. However, when I run the file that contains my test using Invoke-Pester "MyTestFile" -CodeCoverage "MyFileUnderTest", I am being prompted to enter a Read-Host value for my test.

My intent is that the test will run automatically without having to enter a Read-Host value. This would be both when calling the test directly (which works currently), and when calling my test file with the CodeCoverage command.

Does anyone know of a way to achieve this?

Edit:

To the first comment I received, I have reviewed Pester's documentation, including this link https://github.com/pester/Pester/wiki/Unit-Testing-within-Modules. I haven't seen any official documentation from Pester regarding using Read-Host however, and used the solution I found in the StackOverflow link at the top of my question.

Source Code for Module Test-Foo function:

function Test-Foo
{
    return (Read-Host "Enter value->");
}
  • You should [check out the documentation](https://github.com/pester/Pester/wiki/Unit-Testing-within-Modules) – Maximilian Burszley May 11 '18 at 17:42
  • Thank you for your response. I've actually read that link, but the main difference is that I haven't seen any of Pester's documents mention mocking Read-Host. The link I provided in my question was the only instance I saw that used it. I have correctly mocked the command and given a return value I believe, so please correct me on the mock format if I am mistaken. I will edit my question to make it clear I have read the link you sent me. – Cameron Weaver May 11 '18 at 17:56
  • `Read-Host` is a command like any other. Is your script using modules? Or a module itself? – Maximilian Burszley May 11 '18 at 17:58
  • It's using a module that I import at the top of the test file. The test as written above works when just executing the test, i.e. it mocks the read-host call my function (that lives in my module) would call normally, and then the Pester test Should statements pass. When I call this test through Invoke-Pester "MyTestFile" -CodeCoverage "MyModuleUnderTest" though, I am getting prompted to enter a Read-Host value instead of the mock handling it for me. – Cameron Weaver May 11 '18 at 18:00
  • Without seeing your source, it's difficult to give you meaningful feedback. – Maximilian Burszley May 11 '18 at 18:03
  • Source now added: function Test-Foo { return (Read-Host "Enter value->"); } – Cameron Weaver May 11 '18 at 18:49
  • If that is the entirety of your function, I would mock `Test-Foo`, not `Read-Host` – Maximilian Burszley May 11 '18 at 18:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170882/discussion-between-cameron-weaver-and-theincorrigible1). – Cameron Weaver May 11 '18 at 20:45

1 Answers1

3

Given your use-case: Module Test-Foo function

function Test-Foo {
    return (Read-Host -Prompt 'Enter value->')
}

I would advise you to instead mock the Test-Foo function:

Context 'MyModule' {
    Mock -ModuleName MyModule Test-Foo { return 'C:\example' }

    It 'gets user input' {
        Test-Foo | Should -Be 'C:\example'
    }
}
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Thanks again for your help. This definitely solves my problem where Read-Host was prompting me to enter a value when calling the test from a CodeCoverage Invoke-Pester call. I may submit an issue to Pester just to let them know that Read-Host behaves oddly when mocked just in case. – Cameron Weaver May 11 '18 at 20:57
  • @CameronWeaver If you do, link it here. I'd like to add my support – Maximilian Burszley May 11 '18 at 20:59