Skip to content

Commit

Permalink
explain the use-case for a before/after function in tests for #34
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Oct 30, 2018
1 parent afbf11a commit 4faf0d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
34 changes: 33 additions & 1 deletion tap-advanced-testing.md
Expand Up @@ -303,6 +303,7 @@ let COINS = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1
];
```
This is a total of **80 coins**; 8 coin types x 10 of each type.

Each time the machine returns coins we need to _remove_ them from the supply
otherwise we could end up giving the _incorrect_ change
Expand Down Expand Up @@ -687,9 +688,40 @@ Don't forget to **`export`** the `sellProduct` function.

#### 5.7.4 What is the State?

During the execution of our last test,
During the execution of our last test for the `sellProduct` function,
the `COINS` array in the Vending Machine has been altered.

Let's write another test to illustrate this.

In your `test/vending-machine.test.js` file add the following code:

```js
tap.test('Check COINS Available Supply in Vending Machine', function (t) {
const coinsAvail = vendingMachine.getCoinsAvail()
t.equal(coinsAvail.length, 76,
'vendingMachine.getCoinsAvail() shows COINS in Vending Machine is ' +
coinsAvail.length);
t.end();
});
```

If you followed all the previous steps the test will pass,
meaning that the `COINS` array in the `vendingMachine`
was _modified_ by the _previous_ test.

![COINS-available-76](https://user-images.githubusercontent.com/194400/47731483-1c9db680-dc5c-11e8-9e22-5fc295ab36d5.png)

The _initial_ state for the `COINS` array
that we defined in step **5.6.2** (_above_)
was **80 Coins**,
but after executing the `sellProduct` test it's **76 coins**.

It both _simulates_ the "***real world***" and creates a testing "_headache_";
modifying the `COINS` array (_the vending machine's coins available "state"_)
is _desirable_ because in the _real world_ each time an item is sold
the state of `COINS` _should_ be updated.
But it means we need to "_reset_" the `COINS` array between _tests_
otherwise we will end up writing _coupled_ ("_co-dependent_" tests.



Expand Down
23 changes: 21 additions & 2 deletions test/vending-machine.test.js
Expand Up @@ -68,9 +68,20 @@ tap.test('Check Initial Supply of Coins in Vending Machine', function (t) {
});

tap.test('sellProduct(215, [200, 100], COINS) returns [50, 20, 10, 5]', function (t) {
const COINS = vendingMachine.getCoinsAvail();
const COINS = [
200, 200, 200, 200, 200, 200, 200, 200, 200, 200,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1
];
vendingMachine.setCoinsAvail(COINS);
const coinsPaid = [200, 100];
const result = vendingMachine.sellProduct(215, coinsPaid, COINS);
const result = vendingMachine.sellProduct(215, coinsPaid,
vendingMachine.getCoinsAvail());
const expected = [50, 20, 10, 5];
t.deepEqual(result, expected,
'sellProduct returns the correct coins as change');
Expand All @@ -79,3 +90,11 @@ tap.test('sellProduct(215, [200, 100], COINS) returns [50, 20, 10, 5]', function
'running the sellProduct function reduces the COINS the vending machine');
t.end();
});

tap.test('Check COINS Available Supply in Vending Machine', function (t) {
const coinsAvail = vendingMachine.getCoinsAvail()
t.equal(coinsAvail.length, 76,
'vendingMachine.getCoinsAvail() shows COINS in Vending Machine is ' +
coinsAvail.length);
t.end();
});

0 comments on commit 4faf0d0

Please sign in to comment.