Skip to content

Commit

Permalink
Fix inspection of stubs and fakes
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed May 8, 2018
1 parent 2c8a300 commit a1262e0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 41 deletions.
12 changes: 6 additions & 6 deletions documentation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Amazing output when a failure happens!

```js
var searchEngine = {
countResults: sinon.stub()
countResults: sinon.stub().named('myStub')
};

searchEngine.countResults
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/unexpected-sinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
85 changes: 51 additions & 34 deletions test/unexpected-sinon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ MyClass.prototype.bar = function () {
throw new Error('oh no');
};

unexpected.addAssertion('<any> to inspect as <any>', function (expect, subject, value) {
expect.errorMode = 'nested';
expect(expect.inspect(subject).toString(), 'to satisfy', value);
});

describe('unexpected-sinon', function () {
var expect, spy;

Expand All @@ -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();
Expand Down Expand Up @@ -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' +
' // }'
);
});
});
Expand All @@ -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()'
);
});
});
Expand Down Expand Up @@ -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;
Expand All @@ -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' +
' // }'
);
});
});
Expand All @@ -976,19 +993,19 @@ 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) {}
stub.throws(new TypeError());
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()'
);
});
});
Expand Down

0 comments on commit a1262e0

Please sign in to comment.