Our First Test
It's time to start writing some tests! The type of tests that we will be writing are called integration tests
. This is where our tests are checking that our application is communicating with external services (in this case a database) in the way we expect.
Integration tests sit above unit tests
in the testing pyramid
. We don't need to write as many, but they are slower and more complex. We are opting to use them here to help build a solid understanding of the relationship between web app and database.
You can read more about the testing pyramid here.
Our First Test
We will keep our first test simple for now. We will POST
an object containing a name
and a genre
to /artists
and expect a 201
status in return.
To keep our test files easier to maintain, we will split them up according to the CRUD
operation they are testing.
As we are testing create
operations on the Artist
table first, we should make a file called tests/artist-create.test.js
:
It should be fairly trivial to pass this test with a controller function in your app.js
, however, we expect this app to grow in size and complexity as we add features. If we put all our code in app.js
, it will quickly become unmaintainable.
Good practice says we should separate our app into routers and controllers.
You will need to:
-
Create a
src/controllers/artist.js
file and write a controller function which returns a201
status code. -
Import your controller into a
src/routes/artist.js
router and define aPOST /
route to connect to your controller. -
Import your
artistRouter
intoapp.js
and direct all/artists
to yourartistController
.
Refer to your express basics
project if you need a quick refresh on creating routers
and controllers
.
Once this test is passing, commit and push your code to Github.
There is a solution to this challenge [here](It's time to start writing some tests! The type of tests that we will be writing are called integration tests
. This is where our tests are checking that our application is communicating with external services (in this case a database) in the way we expect.
Integration tests sit above unit tests
in the testing pyramid
. We don't need to write as many, but they are slower and more complex. We are opting to use them here to help build a solid understanding of the relationship between web app and database.
You can read more about the testing pyramid here.
Our First Test
We will keep our first test simple for now. We will POST
an object containing a name
and a genre
to /artists
and expect a 201
status in return.
To keep our test files easier to maintain, we will split them up according to the CRUD
operation they are testing.
As we are testing create
operations on the Artist
table first, we should make a file called tests/artist-create.test.js
:
It should be fairly trivial to pass this test with a controller function in your app.js
, however, we expect this app to grow in size and complexity as we add features. If we put all our code in app.js
, it will quickly become unmaintainable.
Good practice says we should separate our app into routers and controllers.
You will need to:
-
Create a
src/controllers/artist.js
file and write a controller function which returns a201
status code. -
Import your controller into a
src/routes/artist.js
router and define aPOST /
route to connect to your controller. -
Import your
artistRouter
intoapp.js
and direct all/artists
to yourartistController
.
Refer to your express basics
project if you need a quick refresh on creating routers
and controllers
.
Once this test is passing, commit and push your code to Github.
There is a solution to this challenge here