Skip to content

Commit

Permalink
switched from try-catch to assert.exception()
Browse files Browse the repository at this point in the history
  • Loading branch information
fearphage committed May 26, 2017
1 parent d8dac84 commit 528a7dc
Show file tree
Hide file tree
Showing 12 changed files with 602 additions and 634 deletions.
44 changes: 20 additions & 24 deletions test/assert-test.js
Expand Up @@ -30,17 +30,14 @@ describe("assert", function () {
});

it("supports proxy property", function () {
var failed = false;
var api = { method: function () {} };
api.method.proxy = function () {};
sinonSpy(api, "method");
api.method();
try {

refute.exception(function () {
sinonAssert.calledOnce(api.method);
} catch (e) {
failed = true;
}
assert.isFalse(failed);
});
});

describe(".fail", function () {
Expand All @@ -53,18 +50,14 @@ describe("assert", function () {
});

it("throws exception", function () {
var failed = false;
var exception;

try {
sinonAssert.fail("Some message");
failed = true;
} catch (e) {
exception = e;
}

assert.isFalse(failed);
assert.equals(exception.name, "AssertError");
assert.exception(
function () {
sinonAssert.fail("Some message");
},
{
name: "AssertError"
}
);
});

it("throws configured exception type", function () {
Expand Down Expand Up @@ -1106,11 +1099,14 @@ describe("assert", function () {
includeFail: false
});

try {
assertCalled(sinonSpy()); // eslint-disable-line no-undef
} catch (e) {
assert.equals(e.message, "expected spy to have been called at least once but was never called");
}
assert.exception(
function () {
assertCalled(sinonSpy()); // eslint-disable-line no-undef
},
{
message: "expected spy to have been called at least once but was never called"
}
);
});

it("exposes asserts into object without prefixes", function () {
Expand Down Expand Up @@ -1159,7 +1155,7 @@ describe("assert", function () {

/*eslint consistent-return: "off"*/
this.message = function (method) {
try {
try { // eslint-disable-line no-restricted-syntax
sinonAssert[method].apply(sinonAssert, [].slice.call(arguments, 1));
} catch (e) {
return e.message;
Expand Down
167 changes: 89 additions & 78 deletions test/call-test.js
Expand Up @@ -390,26 +390,30 @@ describe("sinonSpy.call", function () {
it("throws understandable error if no callback is passed", function () {
var call = this.call;

try {
call.yield();
throw new Error();
} catch (e) {
assert.equals(e.message, "spy cannot yield since no callback was passed.");
}
assert.exception(
function () {
call.yield();
},
{
message: "spy cannot yield since no callback was passed."
}
);
});

it("includes stub name and actual arguments in error", function () {
this.proxy.displayName = "somethingAwesome";
this.args.push(23, 42);
var call = this.call;

try {
call.yield();
throw new Error();
} catch (e) {
assert.equals(e.message, "somethingAwesome cannot yield since no callback was passed. " +
"Received [23, 42]");
}
assert.exception(
function () {
call.yield();
},
{
message: "somethingAwesome cannot yield since no callback was passed. "
+ "Received [23, 42]"
}
);
});

it("invokes last argument as callback", function () {
Expand Down Expand Up @@ -484,12 +488,14 @@ describe("sinonSpy.call", function () {
var call = this.call;
var thisObj = { name1: "value1", name2: "value2" };

try {
call.yieldOn(thisObj);
throw new Error();
} catch (e) {
assert.equals(e.message, "spy cannot yield since no callback was passed.");
}
assert.exception(
function () {
call.yieldOn(thisObj);
},
{
message: "spy cannot yield since no callback was passed."
}
);
});

it("includes stub name and actual arguments in error", function () {
Expand All @@ -498,13 +504,15 @@ describe("sinonSpy.call", function () {
var call = this.call;
var thisObj = { name1: "value1", name2: "value2" };

try {
call.yieldOn(thisObj);
throw new Error();
} catch (e) {
assert.equals(e.message, "somethingAwesome cannot yield since no callback was passed. " +
"Received [23, 42]");
}
assert.exception(
function () {
call.yieldOn(thisObj);
},
{
message: "somethingAwesome cannot yield since no callback was passed. " +
"Received [23, 42]"
}
);
});

it("invokes last argument as callback", function () {
Expand Down Expand Up @@ -575,28 +583,30 @@ describe("sinonSpy.call", function () {
it("throws understandable error if no callback is passed", function () {
var call = this.call;

try {
call.yieldTo("success");
throw new Error();
} catch (e) {
assert.equals(e.message, "spy cannot yield to 'success' since no callback was passed.");
}
assert.exception(
function () {
call.yieldTo("success");
},
{
message: "spy cannot yield to 'success' since no callback was passed."
}
);
});

it("includes stub name and actual arguments in error", function () {
this.proxy.displayName = "somethingAwesome";
this.args.push(23, 42);
var call = this.call;

try {
call.yieldTo("success");
throw new Error();
} catch (e) {
assert.equals(
e.message,
"somethingAwesome cannot yield to 'success' since no callback was passed. Received [23, 42]"
);
}
assert.exception(
function () {
call.yieldTo("success");
},
{
message: "somethingAwesome cannot yield to 'success' since no callback was passed. "
+ "Received [23, 42]"
}
);
});

it("invokes property on last argument as callback", function () {
Expand Down Expand Up @@ -665,24 +675,29 @@ describe("sinonSpy.call", function () {
var call = this.call;
var thisObj = { name1: "value1", name2: "value2" };

try {
call.yieldToOn("success", thisObj);
throw new Error();
} catch (e) {
assert.equals(e.message, "spy cannot yield to 'success' since no callback was passed.");
}
assert.exception(
function () {
call.yieldToOn("success", thisObj);
},
{
message: "spy cannot yield to 'success' since no callback was passed."
}
);
});

it("throws understandable error if symbol prop is not found", function () {
if (typeof Symbol === "function") {
var call = this.call;
var symbol = Symbol();

assert.exception(function () {
call.yieldToOn(symbol, {});
}, function (err) {
return err.message === "spy cannot yield to 'Symbol()' since no callback was passed.";
});
assert.exception(
function () {
call.yieldToOn(symbol, {});
},
{
message: "spy cannot yield to 'Symbol()' since no callback was passed."
}
);
}
});

Expand All @@ -692,15 +707,15 @@ describe("sinonSpy.call", function () {
var call = this.call;
var thisObj = { name1: "value1", name2: "value2" };

try {
call.yieldToOn("success", thisObj);
throw new Error();
} catch (e) {
assert.equals(
e.message,
"somethingAwesome cannot yield to 'success' since no callback was passed. Received [23, 42]"
);
}
assert.exception(
function () {
call.yieldToOn("success", thisObj);
},
{
message: "somethingAwesome cannot yield to 'success' since no callback was passed. "
+ "Received [23, 42]"
}
);
});

it("invokes property on last argument as callback", function () {
Expand Down Expand Up @@ -800,21 +815,19 @@ describe("sinonSpy.call", function () {
it("includes exception", function () {
var object = { doIt: sinonStub().throws("TypeError") };

try {
assert.exception(function () {
object.doIt();
}
catch (e) {} // eslint-disable-line no-empty
});

assert.equals(object.doIt.getCall(0).toString().replace(/ at.*/g, ""), "doIt() !TypeError");
});

it("includes exception message if any", function () {
var object = { doIt: sinonStub().throws("TypeError", "Oh noes!") };

try {
assert.exception(function () {
object.doIt();
}
catch (e) {} // eslint-disable-line no-empty
});

assert.equals(object.doIt.getCall(0).toString().replace(/ at.*/g, ""), "doIt() !TypeError(Oh noes!)");
});
Expand Down Expand Up @@ -1182,17 +1195,16 @@ describe("sinonSpy.call", function () {
error.name = y;
throw error;
});
/*eslint-disable no-empty*/
try {

assert.exception(function () {
spy("a", "1");
} catch (ignored) {}
try {
});
assert.exception(function () {
spy("b", "2");
} catch (ignored) {}
try {
});
assert.exception(function () {
spy("b", "3");
} catch (ignored) {}
/*eslint-enable no-empty*/
});

var argSpy1 = spy.withArgs("a");
var argSpy2 = spy.withArgs("b");
Expand Down Expand Up @@ -1222,10 +1234,9 @@ describe("sinonSpy.call", function () {
// Throwing just to make sure it has no effect.
var spy = sinonSpy(sinonStub().throws());
function call() {
try {
assert.exception(function () {
spy();
}
catch (e) {} // eslint-disable-line no-empty
});
}

call();
Expand Down

0 comments on commit 528a7dc

Please sign in to comment.