diff --git a/couscous.yml b/couscous.yml index f38c2c66..0bc2f913 100644 --- a/couscous.yml +++ b/couscous.yml @@ -157,6 +157,9 @@ menu: testing: name: Testing items: + testing-modules: + text: Modules + relativeUrl: testing/modules.html testing-basic: text: Basic relativeUrl: testing/basic.html diff --git a/testing/basic.md b/testing/basic.md index d70d1cc0..7fd53f71 100644 --- a/testing/basic.md +++ b/testing/basic.md @@ -8,7 +8,7 @@ Testing is a critical part of any software application, and Vapor apps should be ## Displacing Droplet Creation Logic -Up to this point, a lot of our documentation has centered around putting our `Droplet` creation logic in `main.swift`. Unfortunately, when testing against our application, this code becomes largely inaccessible. The first thing we'll need to do is break this out. +Up to this point, a lot of our documentation has centered around putting our `Droplet` creation logic in `main.swift`. Unfortunately, when testing against our application, this code becomes largely inaccessible. The first thing we'll need to do is break this out into the `AppLogic` module. Here's an example of my setup file. I name mine `Droplet+Setup.swift`. Here's how it might look: @@ -33,7 +33,7 @@ func load(_ drop: Droplet) throws { ## Updated `main.swift` -Now that we've abstracted our loading logic, we'll need to update our `main.swift` to reflect those changes. Here's how it should look after: +Now that we've abstracted our loading logic, we'll need to update our `main.swift` **in the `App` module** to reflect those changes. Here's how it should look after: ```swift let drop = Droplet(...) @@ -77,6 +77,8 @@ We'll need to import the testable compilation of Vapor to access the `runCommand Now that all of this has been created, we're ready to start testing our application's `Droplet`. Here's how a really basic test might look: ```swift +@testable import AppLogic + func testEndpoint() throws { let drop = try makeTestDroplet() let request = ... @@ -87,4 +89,6 @@ func testEndpoint() throws { } ``` +Notice that now you can use `CMD-U` to run your tests in Xcode with in-line results. In addition, you can run `vapor test` to test your code from the command line. If you choose to use `swift build` instead and you are using MySQL in your app, make sure you add the correct build flags to the call. + Good luck, and happy testing! diff --git a/testing/modules.md b/testing/modules.md new file mode 100644 index 00000000..167af2bc --- /dev/null +++ b/testing/modules.md @@ -0,0 +1,40 @@ +--- +currentMenu: testing-modules +--- + +# Using Multiple Modules For Testing + +Testing a Vapor app gets tricky, and requires some maneuvering of your app targets. + +> [WARNING] Technically this is only necessary if you plan to run your tests on Linux. You can keep your tests in the same module if you want to only run your tests from the command line using `vapor test` + +## **Step 1:** Update Package.swift + +To start, you need to split up your Vapor project into a target called `App`, and a target called `AppLogic`. The App module will only include a `main.swift`, and your `AppLogic` will contain the actual logic for the app. + +```swift +import PackageDescription + +let package = Package( + name: “ProjectName”, + targets: [ + Target(name: "App", dependencies: ["AppLogic"]) + ], + dependencies: [ + .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 3) + ], + exclude: [ + "Config", + "Database", + "Localization", + "Public", + "Resources" + ] +) +``` + +## **Step 2:** Update Tests Folder + +Make sure that your tests folder has a file called `LinuxMain.swift` and a folder called `AppLogicTests`. In your `AppLogicTests`, you can add your testing files like `UserTests.swift`. + +As always, make sure that you regenerate with `vapor xcode -y`