katowulf / mockfirebase

Firebase mock library for writing unit tests (experimental)
157 stars 40 forks source link

TypeError: Cannot read property 'prototype' of undefined #19

Closed mattvv closed 10 years ago

mattvv commented 10 years ago

Hey @katowulf - Loving that you guys are making a mock library for this.

I know this is beta/unstable but i'd thought i'd post anyway.

I'm having trouble following the example you guys listed using proxyquire. When I follow it and run my tests I get:

/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:2142
    var objectProto = Object.prototype;
                            ^
TypeError: Cannot read property 'prototype' of undefined
    at runInContext (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:2142:29)
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:8437:11)
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:8467:3)
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:8469:4)
    at s (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:5:550)
    at /Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:5:601
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:8476:13)
    at Object.MD5 (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:10194:4)
    at s (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:5:550)
    at e (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:5:721)
    at window.MockFirebase (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:5:739)
    at a (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:5:85)
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mockfirebase/dist/mockfirebase.js:5:286)
    at Module._compile (module.js:456:26)
    at Module._extensions..js (module.js:474:10)
    at Object.Module._extensions..js (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/lib/hook.js:102:13)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/spec/serverSpec.js:9:378)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/lib/hook.js:100:20)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at /Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mocha/lib/mocha.js:172:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mocha/lib/mocha.js:169:14)
    at Mocha.run (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mocha/lib/mocha.js:356:31)
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/mocha/bin/_mocha:366:16)
    at Module._compile (module.js:456:26)
    at Module._extensions..js (module.js:474:10)
    at Object.Module._extensions..js (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/lib/hook.js:102:13)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at runFn (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/lib/command/common/run-with-cover.js:114:16)
    at /Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/lib/command/common/run-with-cover.js:232:17
    at /Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/lib/util/file-matcher.js:56:16
    at /Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/lib/util/file-matcher.js:35:9
    at Object.next (/Users/mattvv/Documents/curiousminds/whistle-sms/node_modules/istanbul/node_modules/fileset/lib/fileset.js:41:14)
    at Fileset.EventEmitter.emit (events.js:95:17)
bendrucker commented 10 years ago

Huh. That's really weird. Mind posting your full test file?

mattvv commented 10 years ago

Thanks for the super fast response @bendrucker I'm using node.js + mocha + should.

spec/spec.js

var should = require('should');
var proxyquire = require('proxyquire');
var server = proxyquire('./src/server', {
  firebase: require('mockfirebase').MockFirebase.autoFlush()
});

describe('Server', function() {
  it ('should pass a test', function() {
    true.should.be.true;
  })
});

src/server.js

var Firebase = require('firebase');
var firebaseUrl = process.env.FIREBASE_URL || 'https://redacted.firebaseio.com/';
var api_key = process.env.FIREBASE_API_KEY || 'redacted';
var companiesRef = new Firebase(firebaseUrl);
var companies;

function listenForChanges() {
  companiesRef.auth(api_key, function(error) {
    if (error) {
      console.log('Login Failed!!', error);
    } else {
      companiesRef.on('value', function(snapshot) {
        companies = snapshot.val();
      });
    }
  });
}

listenForChanges();
exports.firebaseUrl = firebaseUrl;
exports.app = app;
exports.listenForChanges = listenForChanges;
bendrucker commented 10 years ago

Realizing now that I screwed things up a little in Node with the way the build process stands right now. Hadn't noticed it because I have my Node apps pinned to an old version. Will pop this in a branch now. If you can confirm it works I'll cut it as 0.3.0.

bendrucker commented 10 years ago

d6f2e97bd7a524494a2039915977649057b48be0 should fix this. Give it a shot by replacing "~0.2.9" in your package.json with git://github.com/katowulf/mockfirebase.git#d6f2e97bd7a524494a2039915977649057b48be0. Will release it to npm as 0.3.0 if it looks good.

mattvv commented 10 years ago

Thanks for the super quick turnaround. Still having issues with that same example. In this case:

/Users/mattvv/Documents/curiousminds/whistle-sms/spec/serverSpec.js:9
uire('../src/server',{firebase:require('mockfirebase').MockFirebase.autoFlush(
                                                                    ^
TypeError: Object function MockFirebase(currentPath, data, parent, name) {
  // represents the fake url
  //todo should unwrap nested paths; Firebase
  //todo accepts sub-paths, mock should too
  this.currentPath = currentPath || 'Mock://';

  // see failNext()
  this.errs = {};

  // used for setPriorty and moving records
  this.priority = null;

  // null for the root path
  this.myName = parent? name : extractName(currentPath);

  // see autoFlush() and flush()
  this.flushDelay = parent? parent.flushDelay : false;
  this.flushQueue = parent? parent.flushQueue : new FlushQueue();

  // stores the listeners for various event types
  this._events = { value: [], child_added: [], child_removed: [], child_changed: [], child_moved: [] };

  // allows changes to be propagated between child/parent instances
  this.parentRef = parent||null;
  this.children = {};
  if (parent) parent.children[this.name()] = this;

  // stores sorted keys in data for priority ordering
  this.sortedDataKeys = [];

  // do not modify this directly, use set() and flush(true)
  this.data = null;
  this._dataChanged(_.cloneDeep(arguments.length > 1? data||null : MockFirebase.DEFAULT_DATA));

  // stores the last auto id generated by push() for tests
  this._lastAutoId = null;

  // turn all our public methods into spies so they can be monitored for calls and return values
  // see jasmine spies: https://github.com/pivotal/jasmine/wiki/Spies
  // the Firebase constructor can be spied on using spyOn(window, 'Firebase') from within the test unit
  for(var key in this) {
    if( !key.match(/^_/) && typeof(this[key]) === 'function' ) {
      spyFactory(this, key);
    }
  }
} has no method 'autoFlush'
    at Object.<anonymous> (/Users/mattvv/Documents/curiousminds/whistle-sms/spec/serverSpec.js:9:266)
bendrucker commented 10 years ago

New errors! Progress-ish. Hang on.

bendrucker commented 10 years ago

So that was a documentation mistake. autoFlush is on the prototype. That example was either wrong to begin with or outdated. Try the updated instructions:

https://github.com/katowulf/mockfirebase/blob/44625ef1af9f8d9bc4d7a01df0a85926466f3e50/README.md#nodebrowserify

mattvv commented 10 years ago

Seems to be at least running the tests now! I'll investigate further early next week when I am back in town :) Thanks for your quick responses!

bendrucker commented 10 years ago

Awesome!