0

I've to run python and shell scripts in my node JS code. But to begin with, i've to mock the script calls. Is there any ways i can mock this python and shell scripts and send my expected response? Kindly share me your thoughts.

Below is the Code i use to call the script. When the PythonShell.run gets executed, i want to mock results and return my preferred response.

function callScript(){ 
            var options = {
            mode: 'text',
            pythonPath: 'path/to/python',
            pythonOptions: ['-u'],
            scriptPath: 'path/to/my/scripts',
            args: ['value1', 'value2', 'value3']
        };

        PythonShell.run('my_script.py', options, function (err, results) {
            if (err) throw err;
            // results is an array consisting of messages collected during execution
            console.log('results: %j', results);
        });
    }
1
  • Share also the code you have until now so people can actually help you Commented Jul 13, 2016 at 15:56

1 Answer 1

1

with sinon i am able to mock the script call(not only script, we can mock any complex logic) and send expected json repsonse.

var sinonSandbox =  sinon.sandbox.create();

sinonSandbox.stub(PythonShell,'run').withArgs(sinon.match.any, sinon.match.any, sinon.match.any).yields(<Json object>)

after execution:

sinonSandbox.restore();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.