FluentTestUtils 可以更轻松地快速设置一个基于 Fluent 的 Vapor 应用,用于库的测试目的。 如果你有一个想要测试的现有 Vapor 应用,请参阅 VaporTestUtils。
FluentTestUtils 可以通过 Swift Package Manager 安装。 要安装,请将以下内容添加到你的 Package.swift 文件中。
let package = Package(
name: "YourProject",
dependencies: [
...
.package(url: "https://github.com/Appsaurus/FluentTestUtils", from: "0.1.0"),
],
targets: [
.testTarget(name: "YourApp", dependencies: ["FluentTestUtils", ... ])
]
)
1. 导入库
import FluentTestUtils
2. 设置你的测试用例
创建一个继承自 FluentTestCase
的测试用例。
可以通过重写 register(services:)
、configure(databases:)
和 configure(migrations:)
分别注册和配置服务、数据库和迁移。
open class ExampleAppTestCase: FluentTestCase{
static var allTests = [
("testLinuxTestSuiteIncludesAllTests", testLinuxTestSuiteIncludesAllTests),
("testExample", testExample) //Reference your tests here for Linux check
]
func testLinuxTestSuiteIncludesAllTests(){
assertLinuxTestCoverage(tests: type(of: self).allTests)
}
let sqlite: SQLiteDatabase = try! SQLiteDatabase(storage: .memory)
open override func register(_ services: inout Services) throws {
try super.register(&services)
try services.register(FluentSQLiteProvider())
services.register(sqlite)
}
open override func configure(databases: inout DatabasesConfig) throws{
try super.configure(databases: &databases)
databases.add(database: sqlite, as: .sqlite)
}
open override func configure(migrations: inout MigrationConfig){
super.configure(migrations: &migrations)
migrations.add(model: ExampleModel.self, database: .sqlite)
migrations.add(model: ExampleSiblingModel.self, database: .sqlite)
migrations.add(model: ExampleChildModel.self, database: .sqlite)
migrations.add(model: ExampleModelSiblingPivot.self, database: .sqlite)
}
func testExample() {
//Implement your test
}
}
如需更多文档,请参阅 [VaporTestUtils] (https://github.com/Appsaurus/VaporTestUtils),因为 FluentTestCase
继承自该软件包的 VaporTestCase
。
我们非常欢迎你为 FluentTestUtils 做出贡献,请查看 CONTRIBUTING 文件以获取更多信息。
FluentTestUtils 在 MIT 许可证下可用。 有关更多信息,请参阅 LICENSE 文件。