2

I am using express module and below is the code of app.js

app.post('/test_url', function(request, response){
    seneca.client({type: 'http',port: '3000',host: 'localhost',protocol: 'http'}).act({role: 'sample_role', cmd: 'save',firstname: request.params.firstname}, function (err, result) {
        console.log("Inside Seneca act");
        response.json(result);
    })
});

Below is the test file where I am writing the test case for above code.

describe("POST /test_url/:firstname", function() {
    it("should return status code 200", function(done) {
        <b>//here I want to mock the call for seneca.client so that I can test if the call has been made with the required parameters.</b>
        <b>//Also I would like to use the above mock object to further mock the call for act so that I can check if the act method has been called with the required parameters.'</b>
        //Main purpose behind doing so is that I do not want the seneca methods to get actually called, and only want to test if the call has been made.

        request.post("http://localhost:3000/test_url/sara", function(error, response, body) {
            //some verification method on the mock object so as to confirm that both the calls i.e 'seneca.client' and 'seneca.client().act' have been called with the appropriate parameters
            expect(body).toContain("success");              
            done();
        });
    });
});

I tried to mock the seneca calls using jasmine spy and sinon but still the call was actually being going to the method and the the callback function was also invoked resulting in the console.log("Inside Seneca act"); being called, which is not what I expect.

describe("POST /test_url/:firstname", function() {
    it("should return status code 200", function(done) {
        var senecaCall = sinon.stub(seneca, 'client');
        //or spyOn(seneca, "client");

        request.post("http://localhost:3000/test_url/sara", function(error, response, body) {       
            expect(body).toContain("success");              
            done();
        });
    });
});
Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
shubhangi
  • 21
  • 1

0 Answers0