Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stubbed method is not a function #10

Closed
dKab opened this issue Mar 23, 2016 · 1 comment
Closed

stubbed method is not a function #10

dKab opened this issue Mar 23, 2016 · 1 comment

Comments

@dKab
Copy link

dKab commented Mar 23, 2016

this is my tested file:

var restConfigUtil = require('../../../../common/utilities/rest-configuration-utility'),
restService    = require('../../../../base/services/falcon-rest-service'),
querystring = require('querystring');
module.exports = function getPostUnpostResources(req, callback) {
    var restConfig =  restConfigUtil.createWfcAuthenticatedRestConfig('schedule.postunpost.resources',   req);
    restConfig.path += '?' + querystring.stringify(req.body.params);
    restService.executeHttpRequestAsync(restConfig, req.body.data).then(function (data) {
        callback(null, data);
    }, function(err) {
        callback(err);
    });
};

and this is test:

var should = require('should'),
sinon = require('sinon'),
Promise = require('bluebird'),
mockrequire = require('mockrequire');

describe('Schedule - Action - post-unpost - resources', function() {
    describe('get function', function() {
        var callbackSpy, data = ['foo'],
            error = {message: 'some error!'},
            controller;

        //beforeAll
        before(function() {

            var resolvedPromise = Promise.resolve(data),
                rejectedPromise = Promise.reject(error);
            var mockedCall = sinon.stub();

            mockedCall.onCall(0).returns(resolvedPromise);
            mockedCall.onCall(1).returns(rejectedPromise);
            var mockCreateConfig = sinon.stub();
            mockCreateConfig.returns({path: ''});
            controller = mockrequire('../../schedule/controller/actions/post-unpost/resources', {
                '../../../../common/utilities/rest-configuration-utility': {
                    executeHttpRequestAsync: mockedCall
                },
                '../../../../base/services/falcon-rest-service': {
                    createWfcAuthenticatedRestConfig:  mockCreateConfig
                }
            });
        });

        beforeEach(function() {
            callbackSpy = sinon.spy();
        });


    it('should call callback with data returned from backend in case of success', function() {
        try {
            controller({}, callbackSpy);
        } catch(e) {
            throw Error(e.message);
        }

        callbackSpy.args[0].should.be.exactly([null, data]);
    });

Test fails with error:
Error: restConfigUtil.createWfcAuthenticatedRestConfig is not a function

What am I doing wrong?

@mateodelnorte
Copy link
Owner

Your mocked functions don't match the modules your mocking. You have them flipped. Looks like you want:

    controller = mockrequire('../index', {
        '../../../../common/utilities/rest-configuration-utility': {
            createWfcAuthenticatedRestConfig:  mockCreateConfig
        },
        '../../../../base/services/falcon-rest-service': {
            executeHttpRequestAsync: mockedCall
        }

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

No branches or pull requests

2 participants