Testing RESTful routes
So far, we have added tests for routes. Now, let's take a look at how we can test our ShoppingListController
. To test our controller, we would need to write test for each of the RESTful actions and assert that the result at the end of the action is as expected. To make our code modular, it would be good to create a separate test file for these tests. So, to get started writing tests for our Shopping List Controller, follow these steps:
- Create a new empty file called
ShoppingListControllerTests.swift
inside theTests/AppTests
folder and make sure that you add it to theAppTests
target. - Inside this file, add the following lines of code; this will import the required modules for testing, and we will define our test class:
import XCTest import Foundation import Testing import HTTP @testable import Vapor @testable import App class ShoppingListControllerTests: TestCase { let drop = try! Droplet.testable() override func tearDown() { super.tearDown() try! ShoppingList...