Authentication Feature Testing in REST API
Learn how to test the authentication feature in the REST API application.
We'll cover the following...
Test the application
The testing database is already prepared for testing. To create a test, we add a file called api_test.go
in the project’s root directory. After creating the file, we add a function called newApp()
to create an application for testing.
// newApp returns applicationfunc newApp() *fiber.App {// create a new applicationvar app *fiber.App = NewFiberApp()// connect to the testing databasedatabase.InitDatabase(utils.GetValue("DB_TEST_NAME"))// return the applicationreturn app}
In the code above, the application for testing purposes is created. This function is similar to the main
function inside the main.go
file. The difference in this case is that the testing database is used instead of the actual application database.
Before creating the first test, we create the helper function to get the faker or sample data for the item entity. We add a helper function called getItem()
after the newApp()
function declaration.
// getItem returns sample data for item entityfunc getItem() models.Item {// connect to the test databasedatabase.InitDatabase(utils.GetValue("DB_TEST_NAME"))// seed the item data to the test database// the sample data is stored in "item" variableitem, err := database.SeedItem()if err != nil {panic(err)}// return the sample data for item entityreturn item}
As seen in the code above, the getItem()
function is created to get the sample data for the item entity. The sample item data is inserted into the database with the SeedItem()
function. The SeedItem()
function returns the sample data for the item entity that is stored inside the ...