From 30f97b92836378580209909048738d978f2bc3c5 Mon Sep 17 00:00:00 2001 From: Jimmy School Date: Wed, 11 Jan 2017 15:52:14 -0600 Subject: [PATCH 1/4] Update couscous.yml --- couscous.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/couscous.yml b/couscous.yml index 2f5d84fc..cb69e35a 100644 --- a/couscous.yml +++ b/couscous.yml @@ -154,6 +154,9 @@ menu: testing: name: Testing items: + testing-modules: + text: Modules + relativeUrl: testing/modules.html testing-basic: text: Basic relativeUrl: testing/basic.html From 559a85d4876b72af273c3fc26cf60947af3797e7 Mon Sep 17 00:00:00 2001 From: Jimmy School Date: Wed, 11 Jan 2017 15:58:30 -0600 Subject: [PATCH 2/4] add modules --- testing/modules.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 testing/modules.md diff --git a/testing/modules.md b/testing/modules.md new file mode 100644 index 00000000..eb96ccad --- /dev/null +++ b/testing/modules.md @@ -0,0 +1,38 @@ +--- +currentMenu: testing-modules +--- + +# Using Multiple Modules For Testing + +Testing a Vapor app gets tricky, and requires some maneuvering of your app targets. + +## **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` From c75e506629952d5c83474ef3503b0a762b875866 Mon Sep 17 00:00:00 2001 From: Jimmy School Date: Wed, 11 Jan 2017 16:02:18 -0600 Subject: [PATCH 3/4] update testing section --- testing/basic.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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! From 784295c4d29e480519e9f4e2e6db26fdfe9508da Mon Sep 17 00:00:00 2001 From: Jimmy School Date: Wed, 11 Jan 2017 16:11:20 -0600 Subject: [PATCH 4/4] add note --- testing/modules.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/modules.md b/testing/modules.md index eb96ccad..167af2bc 100644 --- a/testing/modules.md +++ b/testing/modules.md @@ -6,6 +6,8 @@ currentMenu: testing-modules 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.