Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CJSify mock tests #1087

Merged
merged 1 commit into from
Jul 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions test/mock-test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
"use strict";

var referee = require("referee");
var sinon = require("../lib/sinon");
var sinonMock = require("../lib/sinon/mock");
var sinonExpectation = require("../lib/sinon/mock-expectation");
var sinonMatch = require("../lib/sinon/match");
var sinonStub = require("../lib/sinon/stub");
var sinonSpy = require("../lib/sinon/spy");
var assert = referee.assert;
var refute = referee.refute;

describe("sinon.mock", function () {
describe("sinonMock", function () {
describe(".create", function () {
it("returns function with expects method", function () {
var mock = sinon.mock.create({});
var mock = sinonMock.create({});

assert.isFunction(mock.expects);
});

it("throws without object", function () {
assert.exception(function () {
sinon.mock.create();
sinonMock.create();
}, "TypeError");
});
});

describe(".expects", function () {
beforeEach(function () {
this.mock = sinon.mock.create({ someMethod: function () {} });
this.mock = sinonMock.create({ someMethod: function () {} });
});

it("throws without method", function () {
Expand Down Expand Up @@ -52,18 +56,18 @@ describe("sinon.mock", function () {
describe(".expectation", function () {
beforeEach(function () {
this.method = "myMeth";
this.expectation = sinon.expectation.create(this.method);
this.expectation = sinonExpectation.create(this.method);
});

it("creates unnamed expectation", function () {
var anonMock = sinon.expectation.create();
var anonMock = sinonExpectation.create();
anonMock.never();

assert(anonMock.verify());
});

it("uses 'anonymous mock expectation' for unnamed expectation", function () {
var anonMock = sinon.expectation.create();
var anonMock = sinonExpectation.create();
anonMock.once();

try {
Expand Down Expand Up @@ -311,7 +315,7 @@ describe("sinon.mock", function () {

it("should not throw when exceeding at least expectation", function () {
var obj = { foobar: function () {} };
var mock = sinon.mock(obj);
var mock = sinonMock(obj);
mock.expects("foobar").atLeast(1);

obj.foobar();
Expand All @@ -324,7 +328,7 @@ describe("sinon.mock", function () {

it("should not throw when exceeding at least expectation and withargs", function () {
var obj = { foobar: function () {} };
var mock = sinon.mock(obj);
var mock = sinonMock(obj);

mock.expects("foobar").withArgs("arg1");
mock.expects("foobar").atLeast(1).withArgs("arg2");
Expand Down Expand Up @@ -535,7 +539,7 @@ describe("sinon.mock", function () {
});

it("works with sinon matchers", function () {
this.expectation.withArgs(sinon.match.number, sinon.match.string, sinon.match.func);
this.expectation.withArgs(sinonMatch.number, sinonMatch.string, sinonMatch.func);
this.expectation(1, "test", function () {});

assert(this.expectation.met());
Expand All @@ -544,7 +548,7 @@ describe("sinon.mock", function () {
it("throws when sinon matchers fail", function () {
var expectation = this.expectation;

this.expectation.withArgs(sinon.match.number, sinon.match.string, sinon.match.func);
this.expectation.withArgs(sinonMatch.number, sinonMatch.string, sinonMatch.func);
assert.exception(function () {
expectation(1, 2, 3);
}, "ExpectationError");
Expand Down Expand Up @@ -639,7 +643,7 @@ describe("sinon.mock", function () {

it("throws if calls on wrong Symbol", function () {
if (typeof Symbol === "function") {
var expectation = sinon.expectation.create("method");
var expectation = sinonExpectation.create("method");
expectation.on(Symbol());

assert.exception(function () {
Expand All @@ -653,14 +657,14 @@ describe("sinon.mock", function () {

describe(".verify", function () {
it("pass if met", function () {
sinon.stub(sinon.expectation, "pass");
sinonStub(sinonExpectation, "pass");
var expectation = this.expectation;

expectation();
expectation.verify();

assert.equals(sinon.expectation.pass.callCount, 1);
sinon.expectation.pass.restore();
assert.equals(sinonExpectation.pass.callCount, 1);
sinonExpectation.pass.restore();
});

it("throws if not called enough times", function () {
Expand Down Expand Up @@ -689,7 +693,7 @@ describe("sinon.mock", function () {
beforeEach(function () {
this.method = function () {};
this.object = { method: this.method };
this.mock = sinon.mock.create(this.object);
this.mock = sinonMock.create(this.object);
});

it("restores mocks", function () {
Expand All @@ -701,14 +705,14 @@ describe("sinon.mock", function () {
});

it("passes verified mocks", function () {
sinon.stub(sinon.expectation, "pass");
sinonStub(sinonExpectation, "pass");

this.mock.expects("method").once();
this.object.method();
this.mock.verify();

assert.equals(sinon.expectation.pass.callCount, 1);
sinon.expectation.pass.restore();
assert.equals(sinonExpectation.pass.callCount, 1);
sinonExpectation.pass.restore();
});

it("restores if not met", function () {
Expand Down Expand Up @@ -886,7 +890,7 @@ describe("sinon.mock", function () {
});

it("does not call pass if no expectations", function () {
var pass = sinon.stub(sinon.expectation, "pass");
var pass = sinonStub(sinonExpectation, "pass");

var mock = this.mock;
mock.expects("method").never();
Expand All @@ -904,7 +908,7 @@ describe("sinon.mock", function () {
beforeEach(function () {
this.method = function () {};
this.object = { method: this.method };
this.mock = sinon.mock.create(this.object);
this.mock = sinonMock.create(this.object);
});

it("mocks object method", function () {
Expand Down Expand Up @@ -960,7 +964,7 @@ describe("sinon.mock", function () {
this.thisValue = {};
this.method = function () {};
this.object = { method: this.method };
this.mock = sinon.mock.create(this.object);
this.mock = sinonMock.create(this.object);
this.mock.expects("method");
this.mock.expects("method").on(this.thisValue);
});
Expand Down Expand Up @@ -994,7 +998,7 @@ describe("sinon.mock", function () {

it("allows mock calls in any order", function () {
var object = { method: function () {} };
var mock = sinon.mock(object);
var mock = sinonMock(object);
mock.expects("method").once().withArgs(42);
mock.expects("method").twice().withArgs("Yeah");

Expand Down Expand Up @@ -1022,15 +1026,15 @@ describe("sinon.mock", function () {

describe("mock function", function () {
it("returns mock method", function () {
var mock = sinon.mock();
var mock = sinonMock();

assert.isFunction(mock);
assert.isFunction(mock.atLeast);
assert.isFunction(mock.verify);
});

it("returns mock object", function () {
var mock = sinon.mock({});
var mock = sinonMock({});

assert.isObject(mock);
assert.isFunction(mock.expects);
Expand All @@ -1040,16 +1044,16 @@ describe("sinon.mock", function () {

describe(".yields", function () {
it("invokes only argument as callback", function () {
var mock = sinon.mock().yields();
var spy = sinon.spy();
var mock = sinonMock().yields();
var spy = sinonSpy();
mock(spy);

assert(spy.calledOnce);
assert.equals(spy.args[0].length, 0);
});

it("throws understandable error if no callback is passed", function () {
var mock = sinon.mock().yields();
var mock = sinonMock().yields();

try {
mock();
Expand Down