node.js - nodejs url.parse test returns undefined -
this might stupid question, there reason why url.parse
return undefined
in case, if pass in url string, proto.getprotocol("http://www.some.com/test")
?
please bare in mind novice nodejs
.
'use strict'; var url = require("url"); var proto = {}; proto.getprotocol = function (path) { console.log(path); var parts = url.parse(path, true); console.log(parts); return parts; }; module.exports = proto;
console.log(parts);
returns undefined is, when run tests.
test:
var chai = require('chai'); var sinonchai = require("sinon-chai"); var expect = chai.expect; var extend = require('lodash').extend; var sinon = require('sinon'); chai.use(sinonchai); var proxyquire = require('proxyquire'); describe("getprotocol", function () { var testedmodule, parsespy, path; before(function () { path = "http://www.some.com/test"; parsespy = sinon.spy(); testedmodule = proxyquire('../getprotocol.js', { 'url': { 'parse': parsespy } }); testedmodule.getprotocol(path); }); it("calls url.parse", function () { expect(parsespy).has.been.calledonce.and.calledwithexactly(path, true); }); });
you using proxyquire
replace getprotocol
's url.parse
method function not url parsing (i.e., sinon spy function, produced sinon.spy()
). spy function can tell if has been called, knows nothing parings urls.
you meant sinon.spy(require("url"), "parse")
, produces spy function calls require("url").parse
, returns result. contrast, function returned sinon.spy()
nothing, except remember how has been called.
Comments
Post a Comment