jeudi 7 mai 2015

Javascript testing, node + sinon, testing 'new' calls

I am trying to test a function that looks like so

ContentModel.prototype.fileHandlers = function() {
if (_.isUndefined(this.__cache__.fileHandler)) {
    this.__cache__.fileHandlers = new FileHandlers(this.__data__.fileHandlers);
}
return this.__cache__.fileHandlers;
 };

to simply check that it caches how I have it set up like so

   it("should return cached the second time.", function() {
            contentModel = new ContentModel({
                fileHandlers: {}
            });
            var firstTime = contentModel.fileHandlers();
            var secondTime = contentModel.fileHandlers();

            expect(firstTime).to.equal(secondTime);
        });

And getting the errors of :

    AssertionError: expected { Object (__data__, __cache__) } to equal { Object (__data__, __cache__) }
  + expected - actual

I just want to basically check the second call is the same - so it's cached when I use the new ContentModel. Can't seem to figure out how to wrestle down this problem. It's sort of an odd problem, but I am going for as much coverage as possible. Thanks!

Just to clarify a little further - I can change .to.equal to to.deep.equal and the test will pass, however I want to check if the object is the same object being returned, not the content.

Aucun commentaire:

Enregistrer un commentaire