diff --git a/documentation/index.md b/documentation/index.md index 254727d..fa2a759 100644 --- a/documentation/index.md +++ b/documentation/index.md @@ -62,7 +62,7 @@ Amazing output when a failure happens! ```js var searchEngine = { - countResults: sinon.stub() + countResults: sinon.stub().named('myStub') }; searchEngine.countResults @@ -78,17 +78,17 @@ expect(searchEngine.countResults, 'to have calls satisfying', function () { ``` ```output -expected stub to have calls satisfying -stub( 'Open source rocks!' ); -stub( 'Open source sucks!' ); +expected myStub to have calls satisfying +myStub( 'Open source rocks!' ); +myStub( 'Open source sucks!' ); -stub( +myStub( 'Open source rocks' // should equal 'Open source rocks!' // // -Open source rocks // +Open source rocks! ); at theFunction (theFileName:xx:yy) -stub( +myStub( 'Open source sucks' // should equal 'Open source sucks!' // // -Open source sucks diff --git a/lib/unexpected-sinon.js b/lib/unexpected-sinon.js index 921b3ce..d7c1e59 100644 --- a/lib/unexpected-sinon.js +++ b/lib/unexpected-sinon.js @@ -333,7 +333,7 @@ identify: isSpy, inspect: function (value, depth, output) { var idNum = parseInt(value.id.replace(/^[^#]+#/, ''), 10); - var label = value.displayName + (value.displayName === 'spy' ? idNum : ''); + var label = value.displayName + (/^(?:spy|stub|fake)$/.test(value.displayName) ? idNum : ''); if (output.colorByIndex) { output.colorByIndex(label, idNum); } else { diff --git a/test/unexpected-sinon.spec.js b/test/unexpected-sinon.spec.js index 7cb6096..8129a64 100644 --- a/test/unexpected-sinon.spec.js +++ b/test/unexpected-sinon.spec.js @@ -11,6 +11,11 @@ MyClass.prototype.bar = function () { throw new Error('oh no'); }; +unexpected.addAssertion(' to inspect as ', function (expect, subject, value) { + expect.errorMode = 'nested'; + expect(expect.inspect(subject).toString(), 'to satisfy', value); +}); + describe('unexpected-sinon', function () { var expect, spy; @@ -20,6 +25,18 @@ describe('unexpected-sinon', function () { spy = sinon.spy().named('spy1'); }); + it('should inspect a spy correctly', function () { + expect(spy, 'to inspect as', /^spy\d+$/); + }); + + it('should inspect a stub correctly', function () { + expect(sinon.stub(), 'to inspect as', /^stub\d+$/); + }); + + it('should inspect a fake correctly', function () { + expect(sinon.fake(), 'to inspect as', /^fake\d+$/); + }); + describe('was called', function () { it('passes if spy was called at least once', function () { spy(); @@ -845,23 +862,23 @@ describe('unexpected-sinon', function () { it('fails if the spy never threw an exception of the given type', function () { expect(function () { - var stub = sinon.stub(); + var stub = sinon.stub().named('myStub'); stub.throws('Error'); try { stub(); } catch (e) {} expect(stub, 'threw', { name: 'TypeError' }); }, 'to throw exception', - "expected stub threw { name: 'TypeError' }\n" + + "expected myStub threw { name: 'TypeError' }\n" + '\n' + - "stub(); at theFunction (theFileName:xx:yy) // expected: threw { name: 'TypeError' }\n" + - " // expected Error() to satisfy { name: 'TypeError' }\n" + - ' //\n' + - ' // {\n' + - " // message: '',\n" + - " // name: 'Error' // should equal 'TypeError'\n" + - ' // //\n' + - ' // // -Error\n' + - ' // // +TypeError\n' + - ' // }' + "myStub(); at theFunction (theFileName:xx:yy) // expected: threw { name: 'TypeError' }\n" + + " // expected Error() to satisfy { name: 'TypeError' }\n" + + ' //\n' + + ' // {\n' + + " // message: '',\n" + + " // name: 'Error' // should equal 'TypeError'\n" + + ' // //\n' + + ' // // -Error\n' + + ' // // +TypeError\n' + + ' // }' ); }); }); @@ -877,15 +894,15 @@ describe('unexpected-sinon', function () { it('fails if the spy never threw the given exception', function () { expect(function () { - var stub = sinon.stub(); + var stub = sinon.stub().named('myStub'); stub.throws(new TypeError()); try { stub(); } catch (e) {} expect(stub, 'threw', new Error()); }, 'to throw exception', - 'expected stub threw Error()\n' + + 'expected myStub threw Error()\n' + '\n' + - 'stub(); at theFunction (theFileName:xx:yy) // expected: threw Error()\n' + - ' // expected TypeError() to satisfy Error()' + 'myStub(); at theFunction (theFileName:xx:yy) // expected: threw Error()\n' + + ' // expected TypeError() to satisfy Error()' ); }); }); @@ -933,7 +950,7 @@ describe('unexpected-sinon', function () { it('fails if the spy did not always threw an exception of the given type', function () { expect(function () { - var stub = sinon.stub(); + var stub = sinon.stub().named('myStub'); var callNumber = 0; stub.callsFake(function () { callNumber += 1; @@ -947,19 +964,19 @@ describe('unexpected-sinon', function () { try { stub(); } catch (e) {} expect(stub, 'always threw', { name: 'Error' }); }, 'to throw exception', - "expected stub always threw { name: 'Error' }\n" + + "expected myStub always threw { name: 'Error' }\n" + '\n' + - 'stub(); at theFunction (theFileName:xx:yy)\n' + - "stub(); at theFunction (theFileName:xx:yy) // expected: threw { name: 'Error' }\n" + - " // expected TypeError() to satisfy { name: 'Error' }\n" + - ' //\n' + - ' // {\n' + - " // message: '',\n" + - " // name: 'TypeError' // should equal 'Error'\n" + - ' // //\n' + - ' // // -TypeError\n' + - ' // // +Error\n' + - ' // }' + 'myStub(); at theFunction (theFileName:xx:yy)\n' + + "myStub(); at theFunction (theFileName:xx:yy) // expected: threw { name: 'Error' }\n" + + " // expected TypeError() to satisfy { name: 'Error' }\n" + + ' //\n' + + ' // {\n' + + " // message: '',\n" + + " // name: 'TypeError' // should equal 'Error'\n" + + ' // //\n' + + ' // // -TypeError\n' + + ' // // +Error\n' + + ' // }' ); }); }); @@ -976,7 +993,7 @@ describe('unexpected-sinon', function () { it('fails if the spy did not always threw the given exception', function () { expect(function () { - var stub = sinon.stub(); + var stub = sinon.stub().named('myStub'); var error = new Error(); stub.throws(error); try { stub(); } catch (e) {} @@ -984,11 +1001,11 @@ describe('unexpected-sinon', function () { try { stub(); } catch (e) {} expect(stub, 'always threw', error); }, 'to throw exception', - 'expected stub always threw Error()\n' + + 'expected myStub always threw Error()\n' + '\n' + - 'stub(); at theFunction (theFileName:xx:yy)\n' + - 'stub(); at theFunction (theFileName:xx:yy) // expected: threw Error()\n' + - ' // expected TypeError() to satisfy Error()' + 'myStub(); at theFunction (theFileName:xx:yy)\n' + + 'myStub(); at theFunction (theFileName:xx:yy) // expected: threw Error()\n' + + ' // expected TypeError() to satisfy Error()' ); }); });