From 9d310e109e2d69658ce9ffd328b06a590d8a1d4f Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 09:55:38 -0400 Subject: [PATCH 01/10] Add steps to get AppLogic target working --- 1.5/testing/modules.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/1.5/testing/modules.md b/1.5/testing/modules.md index 167af2bc..83eb2b24 100755 --- a/1.5/testing/modules.md +++ b/1.5/testing/modules.md @@ -12,13 +12,16 @@ Testing a Vapor app gets tricky, and requires some maneuvering of your app targe 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. +Add a `Sources/AppLogic` folder to your project, then add `targets` to your `Package.swift`: + ```swift import PackageDescription let package = Package( name: “ProjectName”, targets: [ - Target(name: "App", dependencies: ["AppLogic"]) + Target(name: "App", dependencies: ["AppLogic"]), + Target(name: "AppLogic") ], dependencies: [ .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 3) From d2a447f10050815f08a34cb00a41d3a5ed6cc0fc Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 09:55:50 -0400 Subject: [PATCH 02/10] Update vapor minor version in testing doc --- 1.5/testing/modules.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1.5/testing/modules.md b/1.5/testing/modules.md index 83eb2b24..b132dac0 100755 --- a/1.5/testing/modules.md +++ b/1.5/testing/modules.md @@ -24,7 +24,7 @@ let package = Package( Target(name: "AppLogic") ], dependencies: [ - .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 3) + .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 5) ], exclude: [ "Config", From 6d1020264729a68bb4407a7ae435d6056a921bb9 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 09:58:04 -0400 Subject: [PATCH 03/10] Add note about creating `Tests` folder --- 1.5/testing/modules.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/1.5/testing/modules.md b/1.5/testing/modules.md index b132dac0..e7b64ee3 100755 --- a/1.5/testing/modules.md +++ b/1.5/testing/modules.md @@ -38,6 +38,8 @@ let package = Package( ## **Step 2:** Update Tests Folder +If you don't already have a `Tests` folder at the root of your project, add one. + 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 33347dc5597cdf5e8e9bc8171c809c5ee9f86980 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 10:02:15 -0400 Subject: [PATCH 04/10] Confirm the test target users should see --- 1.5/testing/modules.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/1.5/testing/modules.md b/1.5/testing/modules.md index e7b64ee3..22400dc3 100755 --- a/1.5/testing/modules.md +++ b/1.5/testing/modules.md @@ -42,4 +42,6 @@ If you don't already have a `Tests` folder at the root of your project, add one. 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` +As always, make sure that you regenerate with `vapor xcode -y`. + +As long as there is at least one test file under `AppLogicTests`, your generated Xcode project will have an `AppLogicTests` target that you can run as usual. You can also run the tests from the command line with `vapor test`. From e1a7055b437d5d08d1a9ae76fe5b8fd08170d86e Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 10:43:48 -0400 Subject: [PATCH 05/10] Add note about moving other code to AppLogic --- 1.5/testing/basic.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/1.5/testing/basic.md b/1.5/testing/basic.md index 0fc27ce5..06f15b10 100755 --- a/1.5/testing/basic.md +++ b/1.5/testing/basic.md @@ -31,6 +31,8 @@ public func load(_ drop: Droplet) throws { > [WARNING] Do **not** call `run()` anywhere within the `load` function as `run()` is a blocking call. +You will also want to move your application's code into `AppLogic` as well; things like controllers, models, etc. + ## Updated `main.swift` 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: From 5db1439f3da80a4168585abc215e0176b1588818 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 10:44:03 -0400 Subject: [PATCH 06/10] Add imports to get load() accessible --- 1.5/testing/basic.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/1.5/testing/basic.md b/1.5/testing/basic.md index 06f15b10..b873cb51 100755 --- a/1.5/testing/basic.md +++ b/1.5/testing/basic.md @@ -38,6 +38,9 @@ You will also want to move your application's code into `AppLogic` as well; thin 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 +import Vapor +import AppLogic + let drop = Droplet(...) try load(drop) drop.run() From 7df8df553ade10b88380af77ac68829cb8a9049a Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 10:58:27 -0400 Subject: [PATCH 07/10] Clarify adding things under AppLogicTests --- 1.5/testing/basic.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1.5/testing/basic.md b/1.5/testing/basic.md index b873cb51..61e3da93 100755 --- a/1.5/testing/basic.md +++ b/1.5/testing/basic.md @@ -50,7 +50,7 @@ drop.run() ## Testable Droplet -The first thing we'll do is in my testing target, add a file called `Droplet+Test.swift`. It will look like this: +The first thing we'll do is in my testing target `AppLogicTests`, add a file called `Droplet+Test.swift`. It will look like this: ```swift @testable import Vapor @@ -79,7 +79,7 @@ We'll need to import the testable compilation of Vapor to access the `runCommand ## Test Our Droplet -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: +Now that all of this has been created, we're ready to start testing our application's `Droplet` by adding tests under `AppLogicTests`. Here's how a really basic test might look: ```swift @testable import AppLogic From ac62d10d94840685befc4d3fbd425da538e415b7 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 10:59:43 -0400 Subject: [PATCH 08/10] Add complete running test - Code doesn't seem to compile without those headers - Standalone function doesn't seem to run; seems to require being in an XCTestCase class - Adds real request and expected body to make things clearer --- 1.5/testing/basic.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/1.5/testing/basic.md b/1.5/testing/basic.md index 61e3da93..3c6b0c1d 100755 --- a/1.5/testing/basic.md +++ b/1.5/testing/basic.md @@ -82,15 +82,20 @@ 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` by adding tests under `AppLogicTests`. Here's how a really basic test might look: ```swift +import XCTest +import HTTP @testable import AppLogic -func testEndpoint() throws { - let drop = try makeTestDroplet() - let request = ... - let expectedBody = ... +class SmokeTest: XCTestCase { + func testEndpoint() throws { + let drop = try makeTestDroplet() + let request = try Request(method: .get, uri: "/") + let expectedBody = "It works." - let response = try drop.respond(to: request) - XCTAssertEqual(expectedBody, response.body.bytes) + let response = try drop.respond(to: request) + let responseBody = try response.body.bytes!.string() + XCTAssertTrue(responseBody.contains(expectedBody)) + } } ``` From 1c76fc710832c46553849f9352551d5fa247f9d5 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 13:21:04 -0400 Subject: [PATCH 09/10] Change AppLogic/App to App/Run --- 1.5/testing/basic.md | 18 +++++++++--------- 1.5/testing/modules.md | 12 ++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/1.5/testing/basic.md b/1.5/testing/basic.md index 3c6b0c1d..25778409 100755 --- a/1.5/testing/basic.md +++ b/1.5/testing/basic.md @@ -8,9 +8,9 @@ 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 into the `AppLogic` module. +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 and move the running into a new `Run` module. -Here's an example of my setup file. I name mine `Droplet+Setup.swift`. Here's how it might look: +Here's an example of my setup file in `App`. I name mine `Droplet+Setup.swift`. Here's how it might look: ```swift import Vapor @@ -31,15 +31,15 @@ public func load(_ drop: Droplet) throws { > [WARNING] Do **not** call `run()` anywhere within the `load` function as `run()` is a blocking call. -You will also want to move your application's code into `AppLogic` as well; things like controllers, models, etc. - ## Updated `main.swift` -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: +Now that we've abstracted our loading logic, we'll need to move our `main.swift` into the `Run` module. + +Next, we need to update `main.swift` it to reflect those changes. Here's how it should look after: ```swift import Vapor -import AppLogic +import App let drop = Droplet(...) try load(drop) @@ -50,13 +50,13 @@ drop.run() ## Testable Droplet -The first thing we'll do is in my testing target `AppLogicTests`, add a file called `Droplet+Test.swift`. It will look like this: +The first thing we'll do is in my testing target `AppTests`, add a file called `Droplet+Test.swift`. It will look like this: ```swift @testable import Vapor func makeTestDroplet() throws -> Droplet { - let drop = Droplet(arguments: ["dummy/path/", "prepare"], ...) + let drop = Droplet(arguments: ["dummy/path/", "prepare"]) try load(drop) try drop.runCommands() return drop @@ -84,7 +84,7 @@ Now that all of this has been created, we're ready to start testing our applicat ```swift import XCTest import HTTP -@testable import AppLogic +@testable import App class SmokeTest: XCTestCase { func testEndpoint() throws { diff --git a/1.5/testing/modules.md b/1.5/testing/modules.md index 22400dc3..f9eacae0 100755 --- a/1.5/testing/modules.md +++ b/1.5/testing/modules.md @@ -10,9 +10,9 @@ Testing a Vapor app gets tricky, and requires some maneuvering of your app targe ## **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. +To start, you need to split up your Vapor project into a target called `App`, and a target called `Run`. The `Run` module will only include a `main.swift`, and your `App` will contain the actual logic for the app. -Add a `Sources/AppLogic` folder to your project, then add `targets` to your `Package.swift`: +Add a `Sources/Run` folder to your project, then add `targets` to your `Package.swift`: ```swift import PackageDescription @@ -20,8 +20,8 @@ import PackageDescription let package = Package( name: “ProjectName”, targets: [ - Target(name: "App", dependencies: ["AppLogic"]), - Target(name: "AppLogic") + Target(name: "App"), + Target(name: "Run", dependencies: ["App"]) ], dependencies: [ .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 5) @@ -40,8 +40,8 @@ let package = Package( If you don't already have a `Tests` folder at the root of your project, add one. -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`. +Make sure that your tests folder has a file called `LinuxMain.swift` and a folder called `AppTests`. In your `AppTests`, you can add your testing files like `UserTests.swift`. As always, make sure that you regenerate with `vapor xcode -y`. -As long as there is at least one test file under `AppLogicTests`, your generated Xcode project will have an `AppLogicTests` target that you can run as usual. You can also run the tests from the command line with `vapor test`. +As long as there is at least one test file under `AppTests`, your generated Xcode project will have an `AppTests` target that you can run as usual. You can also run the tests from the command line with `vapor test`. From 24e4dacf90c681cbb1a0880810c545399c340b60 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Tue, 25 Apr 2017 13:21:10 -0400 Subject: [PATCH 10/10] Add missing import --- 1.5/testing/basic.md | 1 + 1 file changed, 1 insertion(+) diff --git a/1.5/testing/basic.md b/1.5/testing/basic.md index 25778409..5824f40b 100755 --- a/1.5/testing/basic.md +++ b/1.5/testing/basic.md @@ -54,6 +54,7 @@ The first thing we'll do is in my testing target `AppTests`, add a file called ` ```swift @testable import Vapor +import App func makeTestDroplet() throws -> Droplet { let drop = Droplet(arguments: ["dummy/path/", "prepare"])