How do I mock Perl's built-in backticks operator?

后端 未结 2 1957
再見小時候
再見小時候 2021-01-02 02:26

I\'d like to unit test a Perl program of mine that is using backticks. Is there a way to mock the backticks so that they would do something different from executing the exte

2条回答
  •  粉色の甜心
    2021-01-02 02:55

    Instead of using backticks, you can use capture from IPC::System::Simple, and then write a mock version of capture() in your unit test.

    # application
    use IPC::System::Simple qw(capture);
    my $stuff = capture("some command");
    
    # test script
    {
         package IPC::System::Simple;
         sub capture
         {
             # do something else; perhaps a call to ok()
         }
    }
    
    # ... rest of unit test here
    

提交回复
热议问题