mirror of https://github.com/vapor/docs.git
update testing section
This commit is contained in:
parent
559a85d487
commit
c75e506629
|
|
@ -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!
|
||||
|
|
|
|||
Loading…
Reference in New Issue