Code Monkey home page Code Monkey logo

Comments (6)

neoadventist avatar neoadventist commented on July 28, 2024 7

@wangpin34 did you ever get this to work? Thanks!

from request-promise.

analog-nico avatar analog-nico commented on July 28, 2024

Hi @wangpin34 mocking is indeed an unresolved issue. For alternatives to sinon.js please have a look at issue #55.

I might be able to guess why proxyquire works for Request but not for Request-Promise. To see whether we can fix that could you give me some code snippets? One where you successfully proxyquire'd Request and one where you tried the same for Request-Promise please. Thank you!

from request-promise.

wangpin34 avatar wangpin34 commented on July 28, 2024

@analog-nico I have made mistakes in my unit-test for company business code, I guess. It's a bit of complicated. So I created tiny code snippets for testing this and then I found the issue is not like what I have side previously.

app.js:

var request = require('request-promise');

module.exports = {
    searchMe: function(url) {
        return request(url);
    }
};

test-app.js:

var sinon = require('sinon'),
    proxyquire = require('proxyquire');

describe('Test app function', function() {
    var app,
        request,
        url = 'http://github.com/wangpin34',
        body = JSON.stringify({
            location: 'shanghai'
        });

    before(function() {
        request = sinon.stub();
        app = proxyquire('../lib/app', {
            'request-promise': request
        });
    });

    it('searchme should report me is in shanghai ', function(done) {

        request.withArgs(url).yields(body);

        app.searchMe(url).then(function(data){
            console.log(data);
            done();
        });
    });
});

For above code, I got error when run mocha test:

TypeError: stub expected to yield, but no callback was passed. Received [ht
/github.com/wangpin34]

And test failed.

It seems like problem was caused by sinonjs. And this issue could be closed. Thanks.

PS. Tomorrow I will go into my working code to see why the issue happened in previous post. If I got something, will share it here.

from request-promise.

analog-nico avatar analog-nico commented on July 28, 2024

Thanks @wangpin34. If you replace Request-Promise itself it should always work. I was previously referring to an internal binding between Request-Promise and Request. Anyway, I will extend the README to cover best practices for mocking the lib when I have time.

from request-promise.

wangpin34 avatar wangpin34 commented on July 28, 2024

Thanks you,@analog-nico, and your great work.

from request-promise.

neokaiyuan avatar neokaiyuan commented on July 28, 2024

This also wasn't working for me but I found a workaround with Nock: #55 (comment)

from request-promise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.