This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TransferService { | |
async transfer({ id, sender, receiver, transferAmount, bankName }) { | |
// Validation | |
if (!sender || !receiver || !transferAmount || !bankName) { | |
throw new Error("Some mandatory property was not provided"); | |
} | |
// Handle insufficient credit | |
if (this.options.creditPolicy === "zero" && sender.credit < transferAmount) { | |
this.numberOfDeclined++; //incrementing interal metric |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test("When no credit, the declined transfer does not appear in sender history", () => { | |
// Arrange | |
const transferRequest = testHelpers.factorMoneyTransfer({ | |
sender: { credit: 50 }, | |
transferAmount: 100, | |
}); | |
const transferServiceUnderTest = new TransferService({ creditPolicy: "NoCredit" }); | |
// Act | |
transferServiceUnderTest.transfer(transferRequest); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
+--------------------+------------------+--------------+-------------+ | |
| Criteria | Clean beforeEach | Transactions | No cleanup | | |
+--------------------+------------------+--------------+-------------+ | |
| Speed 🏎 | 6 seconds | 7 seconds | 5.5 seconds | | |
| Troubleshooting 🔍 | One test only | No | All tests | | |
| Chaos 🐒 | No | No | Yes | | |
| Multi-process 🚌🚌 | Harder | Easy | Easy | | |
+--------------------+------------------+--------------+-------------+ | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(() => { | |
db.Orders.destroy({truncate: false, force:true}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test("When deleting an existing order, Then the DB should have 0 records", async () => { | |
// Arrange | |
const orderToAdd = { | |
userId: 1, | |
productId: 2, | |
invoiceId: `123`, //unique field | |
}; | |
await request(expressApp).post("/order").send(orderToAdd); | |
// Act |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ❌ | |
test('Should fail', () => { | |
const transferRequest = testHelpers.factorMoneyTransfer({}); // ❌ | |
serviceUnderTest.options.creditPolicy = 'zero'; // ❌ | |
transferRequest.howMuch = 110; // ❌ | |
// Let's use the library sinon to listen to calls to the DB save function | |
const databaseRepositoryMock = sinon.stub(dbRepository, 'save');//❌ | |
const transferResponse = serviceUnderTest.transfer(transferRequest); | |
expect(transferResponse.currency).toBe('dollar'); // ❌ | |
expect(transferResponse.id).not.toBeNull(); // ❌ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
afterAll(() => { | |
if (isCI) { | |
dockerCompose.down(); | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('When an order error occurs, an email is sent to the admin', () => { | |
//Arrange | |
sinon.stub(dataAccessCode , 'saveOrder').throws(new Error('DB save failed')); | |
const spyOnMailer = sinon.spy(mailer , "sendEmail"); | |
const orderToAdd = {userId: 1, productId: 2, mode: 'approved'}; | |
//Act | |
const receivedResponse = await request(expressApp).post("/order").send(orderToAdd); | |
//Assert |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test("When adding a new valid order , Then should get back 200 response", async () => { | |
//Arrange | |
const orderToAdd = {userId: 1, productId: 2, mode: 'approved'}; | |
nock("http://localhost/user/") | |
.get(`/1`).reply(200, { | |
id: 1, name: "John" | |
}); | |
//Act | |
const receivedAPIResponse = await superTest(expressApp).post("/order").send(orderToAdd); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Open 'mocking' sandbox | |
sinonSandbox = sinon.sandbox.create(); |
NewerOlder