Skip to content

Instantly share code, notes, and snippets.

@Jeffy2012
Created July 10, 2014 09:45
Show Gist options
  • Select an option

  • Save Jeffy2012/2d1f4f8f55177b98464a to your computer and use it in GitHub Desktop.

Select an option

Save Jeffy2012/2d1f4f8f55177b98464a to your computer and use it in GitHub Desktop.

Revisions

  1. Jeffy2012 created this gist Jul 10, 2014.
    186 changes: 186 additions & 0 deletions StubSpec.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,186 @@
    var expect = chai.expect;
    function test() {
    return arguments[0];
    }
    var testJSON = __html__['test/fixtures/test.json'];
    var CID = 1;
    function fn() {
    CID++;
    }
    var obj = {
    test: test,
    call: function (fn, args, context) {
    context = context || null;
    if (Array.isArray(args)) {
    fn.apply(context, args);
    } else {
    fn();
    }
    },
    fn: fn
    };
    describe('Stub Demo', function () {
    describe('var stub = sinon.stub();', function () {
    var stub;
    beforeEach(function () {
    CID = 1;
    stub = sinon.stub();
    });
    afterEach(function () {
    stub.reset();
    });
    it('stub like a spy;', function () {
    stub();
    expect(stub.called).to.equal(true);
    expect(stub.callCount).to.equal(1);
    stub(1);
    expect(stub.calledWith(1)).to.equal(true);
    expect(stub.lastCall.args).to.eql([1])
    });
    it('stub returns value', function () {
    stub.returns(123);
    expect(stub({name: 'Jeffy'})).to.equal(123);
    expect(stub.returned(123)).to.equal(true);
    expect(stub.calledWith({name: 'Jeffy'})).to.equal(true);
    stub.returns({name: 'Jeffy'});
    stub();
    expect(stub.returned({name: 'Jeffy'})).to.equal(true);
    });
    it('stub withArgs', function () {
    stub.withArgs(1, 2, 3).returns({name: 'Jeffy'});
    expect(stub(1, 2, 3)).to.eql({name: 'Jeffy'});
    expect(stub()).to.be.an('undefined');
    expect(stub.callCount).to.equal(2);
    });
    it('stub callsArg(index)', function () {
    stub.returns('Jeffy').callsArg(0);
    expect(stub(fn)).to.equal('Jeffy');
    expect(CID).to.equal(2);
    stub(fn);
    expect(CID).to.equal(3);
    });
    it('stub callsArgOn(index, context);', function () {
    var spy = sinon.spy();
    stub.callsArgOn(0, {name: 'Jeffy'});
    stub(spy);
    expect(spy.called).to.equal(true);
    expect(spy.firstCall.thisValue).to.eql({name: 'Jeffy'});
    });
    it('stub callsArgWith(index, arg1, arg2, ...);');
    it('stub callsArgWithOn(index, context, arg1, arg2, ...);');
    it('stub yields([arg1, arg2, ...]);');
    it('stub yieldsOn(context, [arg1, arg2, ...])');
    it('stub yieldsTo(property, [arg1, arg2, ...])', function () {
    sinon.spy(obj, 'fn');
    stub.yieldsTo('fn');
    stub(obj);
    expect(CID).to.equal(2);
    expect(obj.fn.called).to.equal(true);
    expect(obj.fn.callCount).to.equal(1);
    });
    it('stub yieldsToOn(property, context, [arg1, arg2, ...])');
    it('stub callsArgAsync(index);', function (done) {
    var spy = sinon.spy();
    stub.callsArgAsync(0);
    stub(spy);
    expect(spy.called).to.equal(false);
    done();
    expect(spy.called).to.equal(true);
    expect(CID).to.equal(2);
    });
    });
    describe('var stub = sinon.stub(object, "method");', function () {
    var stub;
    beforeEach(function () {
    stub = sinon.stub(obj, 'test');
    });
    afterEach(function () {
    obj.test.restore();
    });
    it('stub return values', function () {
    stub.returns(123);
    expect(obj.test(1, 2, 3)).to.equal(123);
    expect(obj.test.args[0]).to.eql([1, 2, 3]);
    });
    });

    describe('stub $.ajax', function () {
    var stub;
    beforeEach(function () {
    stub = sinon.stub($, 'ajax');
    });
    afterEach(function () {
    $.ajax.restore();
    });
    it('normal $.ajax success Test', function () {
    var spy = sinon.spy();
    $.ajax.yieldsTo('success', [JSON.parse(testJSON)]);
    $.ajax({
    url: '/test',
    data: {
    name: 'Jeffy'
    },
    success: spy
    });
    expect(spy.called).to.equal(true);
    expect(spy.callCount).to.equal(1);
    expect(spy.calledWith(JSON.parse(testJSON)));
    expect($.ajax.firstCall.args[0].url).to.equal('/test');
    });
    it('normal $.ajax error Test');

    it('promise $.ajax done Test', function () {
    var spy = sinon.spy();
    $.ajax.returns($.Deferred().resolve(JSON.parse(testJSON)));
    $.ajax({
    url: '/test',
    data: {
    name: 'Jeffy'
    }
    }).done(spy).done(function (data) {
    expect(data).to.eql(JSON.parse(testJSON));
    expect(data).to.have.property('name', 'Jeffy');
    });
    expect(spy.called).to.equal(true);
    expect(spy.callCount).to.equal(1);
    expect(spy.calledWith(JSON.parse(testJSON)));
    expect($.ajax.firstCall.args[0].url).to.equal('/test');
    spy.reset();
    $.ajax.reset();
    var model = $.extend($({}),{
    fetch : function () {
    var self = this;
    $.ajax({
    url: '/test',
    data: {
    name: 'Jeffy'
    }
    }).done(function (data) {
    self.trigger('load', [data, self]);
    });
    }
    });

    model.bind('load', spy);
    model.bind('load', function (e, data, model) {
    model.name = data.name;
    model.age = data.age;
    expect(model.name).to.equal('Jeffy');
    });
    model.fetch();

    expect(model.name).to.equal('Jeffy');
    expect(spy.called).to.equal(true);
    expect(spy.args[0][1]).to.eql(JSON.parse(testJSON));
    });
    });
    describe('var stub = sinon.stub(object, "method", func);', function () {
    it('return different value for every call', function () {
    var stub = sinon.stub(obj, "test", function () {
    return arguments[0].toString();
    });
    expect(stub(123)).to.be.a('string');
    expect(stub([12,34])).to.equal('12,34');
    });
    });
    });