Skip to content

Commit

Permalink
Rewrite 2 out of 3 Auction tests to use mockist testing
Browse files Browse the repository at this point in the history
This changes how the tests work as an example of behavior verification,
as opposed to state verification. The third test is still not ported,
see: sinonjs/sinon#1501
  • Loading branch information
rkaw92 committed Nov 16, 2020
1 parent 41f4d2c commit c496fe3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
20 changes: 16 additions & 4 deletions code/in-search-of-a-unit/test/Auction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Money } from "../src/Money";
import * as uuid from 'uuid';
import { Offer } from "../src/Offer";
import * as assert from "assert";
import * as sinon from "sinon";

function testOffer(amount: number, currency = 'PLN', user?: string): Offer {
return {
Expand All @@ -26,25 +27,36 @@ function stuffAuction(auction: Auction, count: number) {
describe('Auction', function() {
it('should immediately reject an offer that could not possibly win', async function() {
const notifier = new EmailNotifier();
const mockNotifier = sinon.mock(notifier);
const sale = new Auction('PLN', 5, notifier);
// Initially, everybody goes into auction at 10 PLN:
const initialOffers = stuffAuction(sale, 5);
// Subsequent offers for the same amount should be rejected:
const sixthOfferResult = await sale.makeOffer(testOffer(10));
assert.strictEqual(sixthOfferResult, false);
const sixthOffer = testOffer(10);
mockNotifier.expects('notifyUser').once().withArgs(sixthOffer.madeByUserID, {
eventType: 'outbid',
offer: sixthOffer
}).resolves();
const sixthOfferResult = await sale.makeOffer(sixthOffer);
mockNotifier.verify();
});
it('should replace the last offer if a better one comes in', async function() {
const notifier = new EmailNotifier();
const mockNotifier = sinon.mock(notifier);
const sale = new Auction('PLN', 5, notifier);
const initialOffers = stuffAuction(sale, 5);
// A new offer should annul the last one made:
const lastOffer = sale.getQualifyingOffers().pop()!;
const betterOffer = testOffer(15);
mockNotifier.expects('notifyUser').once().withArgs(lastOffer.madeByUserID, {
eventType: 'outbid',
offer: lastOffer
}).resolves();
await sale.makeOffer(betterOffer);
assert.ok(sale.getQualifyingOffers().includes(betterOffer));
assert.ok(!sale.getQualifyingOffers().includes(lastOffer));
mockNotifier.verify();
});
it('should select the winners after some offers are replaced', async function() {
// TODO: Rewrite this test to use behavior verification as well!
const notifier = new EmailNotifier();
const sale = new Auction('PLN', 5, notifier);
const initialOffers = stuffAuction(sale, 5);
Expand Down
15 changes: 15 additions & 0 deletions code/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/big.js": "^6.0.0",
"@types/mocha": "^8.0.4",
"@types/node": "^14.14.7",
"@types/sinon": "^9.0.8",
"@types/uuid": "^8.3.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
Expand Down

0 comments on commit c496fe3

Please sign in to comment.