FluentTestApp 使启动并运行可测试的 Vapor 应用程序变得非常简单,还可以选择播种 SQLite 内存数据库。当您在 Fluent 之上构建库并希望快速开始测试代码,跳过所有设置时,这是理想的选择。它包含一个涵盖大多数(希望很快更多)基本数据类型和所有基本关系的 schema。
FluentTestApp 可通过 Swift Package Manager 获取。要安装,请将以下内容添加到您的 Package.swift 文件中。
let package = Package(
name: "YourProject",
dependencies: [
...
.package(url: "https://github.com/Appsaurus/FluentTestApp", from: "0.1.0"),
],
targets: [
.testTarget(name: "YourApp", dependencies: ["FluentTestApp", ... ])
]
)
创建一个继承自 FluentAppTestCase
的测试用例。由于所有内容都在设置期间播种,因此您可以立即开始在测试中进行查询。
import XCTest
import Vapor
import FluentTestApp
import FluentTestModels
class ExampleAppTestCase: FluentAppTestCase {
static var allTests = [
("testLinuxTestSuiteIncludesAllTests", testLinuxTestSuiteIncludesAllTests),
("testQueries", testQueries) //Reference your tests here for Linux check
]
func testLinuxTestSuiteIncludesAllTests(){
assertLinuxTestCoverage(tests: type(of: self).allTests)
}
func testQueries() throws{
XCTAssertEqual(try ExampleModel.query(on: request).count().wait(), ExampleModelsSeeder.exampleModelCount)
XCTAssertEqual(try ExampleSiblingModel.query(on: request).count().wait(), ExampleModelsSeeder.exampleSiblingModelCount)
XCTAssertEqual(try ExampleChildModel.query(on: request).count().wait(), ExampleModelsSeeder.exampleChildModelCount)
}
}
如果您的测试需要设置路由,您可以通过覆盖 routes(_:)
并注册它们来实现。然后,您可以使用 executeRequest(headers:method:body:uri:queryitems:)
轻松地对您的路由执行请求。
override func routes(_ router: Router) throws {
try super.routes(router)
router.get("models") { request in
return ExampleModel.query(on: request).all()
}
}
func testModelsRoute() throws{
let models: [ExampleModel] = try executeRequest(method: .GET, uri: "models").content.syncDecode([ExampleModel].self)
XCTAssertEqual(models.count, ExampleModelsSeeder.exampleModelCount)
}
默认情况下,FluentTestApp 将使用来自 FluentTestModels 的 schema 自动播种 SQLite 内存数据库,该 schema 旨在方便测试基于 Fluent 的库的各个方面。这包括模型之间关系的播种。如果您不希望数据库自动播种,请将 autoSeed
设置为 false
。此外,您可以在 configure(migrations)
中实现您自己的 Seeder。
override open var autoSeed: Bool { return false }
open override func configure(migrations: inout MigrationConfig){
super.configure(migrations: &migrations)
migrations.add(model: YourCustomModel.self, database: .sqlite)
migrations.add(migration: YourCustomModelSeed.self, database: .sqlite)
}
有关如何实现自定义 seeder 的示例,请参阅 FluentSeeder。
我们非常欢迎您为 FluentTestApp 做出贡献,请查看 CONTRIBUTING 文件以获取更多信息。
FluentTestApp 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。