Skip to content

Instantly share code, notes, and snippets.

@biscuitvile
Created September 4, 2013 02:18

Revisions

  1. @tchak tchak renamed this gist Mar 31, 2013. 1 changed file with 19 additions and 4 deletions.
    23 changes: 19 additions & 4 deletions factory.js → container.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,22 @@
    window.Factory = function(namespace) {
    sinon.Container = function(namespace) {
    this.namespace = namespace;
    this._cache = [];
    this._mocks = [];
    this.container = namespace.__container__;
    };

    var mock = function(context, method, args) {
    var object = context[method].apply(context, args);
    return sinon.mock(object);
    return mockObject(context, object);
    };

    Factory.prototype = {
    var mockObject = function(context, object) {
    var mock = sinon.mock(object);
    context._mocks.push(object);
    return mock;
    };

    sinon.Container.prototype = {
    model: function(type, hash) {
    var store = this.store();

    @@ -52,6 +59,10 @@ Factory.prototype = {
    return mock(this, 'store', arguments);
    },

    mock: function(object) {
    return mockObject(this, object);
    },

    restoreObject: function(object) {
    if (object && object.restore) {
    object.restore();
    @@ -64,6 +75,10 @@ Factory.prototype = {
    instance.destroy();
    });
    this._cache = [];
    this._mocks.forEach(function(mock) {
    this.restoreObject(mock);
    }, this);
    this._mocks = [];

    this.container.reset();
    }
    @@ -84,7 +99,6 @@ sinon.mock.expects = function expects(method, isProperty) {
    if (!this.expectations) {
    this.expectations = {};
    this.proxies = [];
    this.restores = {};
    }

    if (!this.expectations[method]) {
    @@ -101,6 +115,7 @@ sinon.mock.expects = function expects(method, isProperty) {
    Ember.defineProperty(object, method, desc);
    };

    this.restores = this.restores || {};
    this.restores[method] = {restore: restore};
    push.call(this.proxies, method);
    }
  2. @tchak tchak revised this gist Mar 6, 2013. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions factory.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    (function() {

    window.Factory = function(namespace) {
    this.namespace = namespace;
    this._cache = [];
    @@ -86,6 +84,7 @@ sinon.mock.expects = function expects(method, isProperty) {
    if (!this.expectations) {
    this.expectations = {};
    this.proxies = [];
    this.restores = {};
    }

    if (!this.expectations[method]) {
    @@ -102,7 +101,8 @@ sinon.mock.expects = function expects(method, isProperty) {
    Ember.defineProperty(object, method, desc);
    };

    push.call(this.proxies, restore);
    this.restores[method] = {restore: restore};
    push.call(this.proxies, method);
    }

    var expectation = sinon.expectation.create(method);
    @@ -112,15 +112,15 @@ sinon.mock.expects = function expects(method, isProperty) {
    };

    sinon.mock.restore = function restore() {
    var object = this.object;
    var object = this.object,
    restores = this.restores || {};

    if (!this.proxies) { return; }

    each.call(this.proxies, function(proxy) {
    if (typeof proxy === 'function') {
    proxy();
    } else if (object[proxy] && typeof object[proxy].restore == "function") {
    object[proxy].restore();
    proxy = restores[proxy] || object[proxy];
    if (proxy && proxy.restore) {
    proxy.restore();
    }
    });
    };

    })();
  3. @tchak tchak revised this gist Jan 25, 2013. 1 changed file with 85 additions and 0 deletions.
    85 changes: 85 additions & 0 deletions factory.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,16 @@
    (function() {

    window.Factory = function(namespace) {
    this.namespace = namespace;
    this._cache = [];
    this.container = namespace.__container__;
    };

    var mock = function(context, method, args) {
    var object = context[method].apply(context, args);
    return sinon.mock(object);
    };

    Factory.prototype = {
    model: function(type, hash) {
    var store = this.store();
    @@ -30,6 +37,30 @@ Factory.prototype = {
    return this.container.lookup('store:main');
    },

    /* Mocks */
    mockModel: function() {
    return mock(this, 'model', arguments);
    },

    mockView: function() {
    return mock(this, 'view', arguments);
    },

    mockController: function() {
    return mock(this, 'controller', arguments);
    },

    mockStore: function() {
    return mock(this, 'store', arguments);
    },

    restoreObject: function(object) {
    if (object && object.restore) {
    object.restore();
    }
    return null;
    },

    reset: function() {
    this._cache.forEach(function(instance) {
    instance.destroy();
    @@ -39,3 +70,57 @@ Factory.prototype = {
    this.container.reset();
    }
    };

    var push = Array.prototype.push,
    each = Array.prototype.forEach,
    _expects = sinon.mock.expects;

    sinon.mock.expects = function expects(method, isProperty) {
    if (!isProperty) {
    return _expects.call(this, method);
    }
    if (!method) {
    throw new TypeError("method is falsy");
    }

    if (!this.expectations) {
    this.expectations = {};
    this.proxies = [];
    }

    if (!this.expectations[method]) {
    this.expectations[method] = [];
    var mockObject = this;
    var object = this.object;
    var desc = Ember.meta(object).descs[method];

    Ember.defineProperty(this.object, method, Ember.computed(function(key, value) {
    return mockObject.invokeMethod(method, this, arguments);
    }));

    var restore = function() {
    Ember.defineProperty(object, method, desc);
    };

    push.call(this.proxies, restore);
    }

    var expectation = sinon.expectation.create(method);
    push.call(this.expectations[method], expectation);

    return expectation;
    };

    sinon.mock.restore = function restore() {
    var object = this.object;

    each.call(this.proxies, function(proxy) {
    if (typeof proxy === 'function') {
    proxy();
    } else if (object[proxy] && typeof object[proxy].restore == "function") {
    object[proxy].restore();
    }
    });
    };

    })();
  4. @tchak tchak created this gist Jan 20, 2013.
    41 changes: 41 additions & 0 deletions factory.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    window.Factory = function(namespace) {
    this.namespace = namespace;
    this._cache = [];
    this.container = namespace.__container__;
    };

    Factory.prototype = {
    model: function(type, hash) {
    var store = this.store();

    type = this.namespace[type];
    return store.createRecord(type, hash || {});
    },

    view: function(name) {
    var view = this.container.lookup('view:'+name);
    this._cache.push(view);
    return view;
    },

    controller: function(name, context) {
    var controller = this.container.lookup('controller:'+name);
    if (controller && context) {
    controller.set('content', context);
    }
    return controller;
    },

    store: function() {
    return this.container.lookup('store:main');
    },

    reset: function() {
    this._cache.forEach(function(instance) {
    instance.destroy();
    });
    this._cache = [];

    this.container.reset();
    }
    };