Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/BinaryBirds/vapor-spec
Unit testing Vapor applications through declarative specifications.
https://github.com/BinaryBirds/vapor-spec
tdd unit-test vapor vapor-4 xctest xctvapor
Last synced: 3 months ago
JSON representation
Unit testing Vapor applications through declarative specifications.
- Host: GitHub
- URL: https://github.com/BinaryBirds/vapor-spec
- Owner: BinaryBirds
- License: mit
- Created: 2020-05-08T14:57:08.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-18T10:47:45.000Z (over 1 year ago)
- Last Synced: 2024-07-16T06:09:36.809Z (4 months ago)
- Topics: tdd, unit-test, vapor, vapor-4, xctest, xctvapor
- Language: Swift
- Homepage: https://binarybirds.com
- Size: 19.5 KB
- Stars: 36
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# VaporSpec
Unit testing [Vapor](https://github.com/vapor/vapor) applications through declarative specifications.
## Install
Add the repository as a dependency:
```swift
.package(url: "https://github.com/binarybirds/vapor-spec", from: "2.0.0"),
```Add VaporSpec to the target dependencies:
```swift
.product(name: "VaporSpec", package: "vapor-spec"),
```Update the packages and you are ready.
## Usage example
### Api
```swift
import XCTest
@testable import VaporSpecfinal class VaporSpecTests: XCTestCase {
func testStatusCode() throws {
let app = Application(.testing)
defer { app.shutdown() }try app
.spec()
.get("foo")
.expect(.notFound)
.test()
}
func testHeaderValues() throws {
let app = Application(.testing)
app.routes.get("hello") { _ in "hello" }
defer { app.shutdown() }try app
.spec()
.get("hello")
.expect("Content-Length", ["5"])
.expect("Content-Type", ["text/plain; charset=utf-8"])
.test()
}
func testContentTypeHeader() throws {
let app = Application(.testing)
app.routes.get("hello") { _ in "hello" }
defer { app.shutdown() }try app
.spec()
.get("hello")
.expect("text/plain; charset=utf-8")
.test()
}
func testBodyValue() throws {
let app = Application(.testing)
app.routes.get("hello") { _ in "hello" }
defer { app.shutdown() }try app
.spec()
.get("hello")
.expect(closure: { res in
let string = res.body.string
XCTAssertEqual(string, "hello")
})
.test()
}
func testJSON() throws {
struct Test: Codable, Content {
let foo: String
let bar: Int
let baz: Bool
}let app = Application(.testing)
app.routes.post("foo") { req in try req.content.decode(Test.self) }
defer { app.shutdown() }let input = Test(foo: "foo", bar: 42, baz: true)
try app
.spec()
.post("foo")
.json(input, Test.self) { res in
XCTAssertEqual(input.foo, res.foo)
XCTAssertEqual(input.bar, res.bar)
XCTAssertEqual(input.baz, res.baz)
}
.test()
}
}
```