danielinoa / spec

Unit testing Vapor 4 applications through declarative specifications.

Home Page:https://theswiftdev.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spec

Unit testing Vapor 4 applications through declarative specifications.

Install

Add the repository as a dependency:

.package(url: "https://github.com/binarybirds/spec.git", from: "1.0.0"),

Add Spec to the target dependencies:

.product(name: "Spec", package: "spec"),

Update the packages and you are ready.

Usage example

Api

@testable import App
import Spec

final class BlogApiTests: XCTestCase {
    
    func testLogin(_ app: Application) throws {

        struct UserLoginRequest: Content {
            let email: String
            let password: String
        }
        struct UserTokenResponse: Content {
            let id: String
            let value: String
        }

        let userBody = UserLoginRequest(email: "foo@bar.com", password: "foo")
        
        var token: String?

        try app
            .describe("Login request should return ok")     // describe spec
            .post("/api/user/login")                        // post to endpoint
            .header("accept", "application/json")           // add header
            .body(userBody)                                 // add content body
            .expect(.ok)                                    // status code
            .expect(.json)                                  // media type
            .expect("content-length", ["81"])               // expect header
            .expect(UserTokenResponse.self) { content in    // expect content
                token = content.value                       // retreive content
            }
            .test(.inMemory)                                // test in memory

        _ = try XCTUnwrap(token)

    }
}

License

WTFPL - Do what the fuck you want to.

About

Unit testing Vapor 4 applications through declarative specifications.

https://theswiftdev.com

License:Other


Languages

Language:Swift 100.0%