mirror of https://github.com/vapor/docs.git
Merge pull request #141 from CodingItWrong/CodingItWrong-testing
Testing docs adjustments
This commit is contained in:
commit
c30c99bb97
|
|
@ -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
|
||||
|
|
@ -33,9 +33,14 @@ public func load(_ drop: Droplet) throws {
|
|||
|
||||
## 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 App
|
||||
|
||||
let drop = Droplet(...)
|
||||
try load(drop)
|
||||
drop.run()
|
||||
|
|
@ -45,13 +50,14 @@ 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 `AppTests`, add a file called `Droplet+Test.swift`. It will look like this:
|
||||
|
||||
```swift
|
||||
@testable import Vapor
|
||||
import App
|
||||
|
||||
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
|
||||
|
|
@ -74,18 +80,23 @@ 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
|
||||
import XCTest
|
||||
import HTTP
|
||||
@testable import App
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +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/Run` folder to your project, then add `targets` to your `Package.swift`:
|
||||
|
||||
```swift
|
||||
import PackageDescription
|
||||
|
|
@ -18,10 +20,11 @@ import PackageDescription
|
|||
let package = Package(
|
||||
name: “ProjectName”,
|
||||
targets: [
|
||||
Target(name: "App", dependencies: ["AppLogic"])
|
||||
Target(name: "App"),
|
||||
Target(name: "Run", dependencies: ["App"])
|
||||
],
|
||||
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",
|
||||
|
|
@ -35,6 +38,10 @@ let package = Package(
|
|||
|
||||
## **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`.
|
||||
If you don't already have a `Tests` folder at the root of your project, add one.
|
||||
|
||||
As always, make sure that you regenerate with `vapor xcode -y`
|
||||
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 `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`.
|
||||
|
|
|
|||
Loading…
Reference in New Issue