测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年5月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✓ |
由 doozMen、Leroy Jenkins、Jelle Vandebeeck 维护。
要快速入门,请遵循以下说明。有关为什么和如何构建 AirRivet 的更多信息,请访问 wiki 页面。
AirRivet 是使用泛型在 Swift 中构建的服务层。
想法是您有一个名为 Air
的类,它为 Environment
执行请求。为此,它需要一个可以传递给 Air
的名为 Rivet
的类型。Rivet
类型呢?
AnyThing
可以是 Rivet
,如果它们是 Rivetable
。《Rivetable》是 Rivet(类型)必须符合的协议的组合。《Rivet》如果是 Rivetable
,则
Mitigatable
:接收请求使任何可能出错的事物减少严重程度。《AirRivet》包括 2 个 Mitigator
MitigatorDefault
打印抛出的任何错误MitigatorNoPrinting
不打印任何内容。适合测试!Parsable
:您会获得字典,您可以使用它们来设置变量。
EnvironmentConfigurable
:我们可以通过 Air
从 生产 环境或 开发 环境中获取数据。
Mockable
:还有一个特殊情况,可以是模拟的环境。那么您的请求将来自本地文件 (模拟文件)
UniqueAble
:如果您的 AnyThing
在一个 集合 中,您可以通过符合 UniqueAble
找到您的实体
Transformable
:我们需要能够将数据 转换 为 Rivetable
类型。默认转换器是 TransformJSON
,但您可以提供自己的。AirRivet 包含您可以使用 3 个转换器
TransformJSON
默认的TransfromCoreData
-> 用于转换到核心数据对象。TransformAndStore
-> 用于截取获取时时的 JSON 文件。TransformAndStoreCoreData
-> 等于 3,但对于/coredata如果执行上述操作(示例中提供了默认实现)。然后您可以像下面这样使用代码。
要运行示例项目,请克隆仓库,然后从 Example 目录中运行 pod install
。
Environment, Mockable
接口的通用类。Rivetable
的模型对象。class Environment <Rivet: EnvironmentConfigurable>: Environment, Mockable {
//You should use the contextPath as your API works. For this Environment we have "<base>/contextPath"
var serverUrl = "http:// ...\(Rivet().contextPath())"
var request: NSMutableURLRequest
init() {
request = NSMutableURLRequest(URL: NSURL(string: serverUrl)!)
}
func shouldMock() -> Bool {
return false
}
}
class Foo: Rivetable {
// Implement protocols.
// See GameScore class in Example project.
}
import AirRivet
do {
try Air.retrieve({ (response : Foo) in
print(response)
})
} catch {
print("💣Error with request construction: \(error)💣")
}
使用它来更改默认的错误处理。默认处理是
打印
错误抛出
错误回调用堆栈class Foo: Foo {
// Implement protocols.
// See GameScore class in Example project.
class override func responseMitigator() -> protocol<ResponseMitigatable, Mitigator> {
return MitigatorDefault()
}
class override func requestMitigator() -> protocol<RequestMitigatable, Mitigator> {
return MitigatorDefault()
}
}
如果想要不同的错误处理,你可以提供不同的 Mititagor 实例。例如,我们提供了 MitigatorNoPrinting
。在单元测试中使用时非常方便。
class MockFoo: Foo {
// Implement protocols.
// See GameScore class in Example project.
// Override to disable printing
class override func responseMitigator() -> protocol<ResponseMitigatable, Mitigator> {
return MitigatorNoPrinting()
}
class override func requestMitigator() -> protocol<RequestMitigatable, Mitigator> {
return MitigatorNoPrinting()
}
}
我们使用了泛型,因此不能直接在 Objective-C 中使用 AirRivet。你可以通过编写包装器来绕过这个问题。
在我们的例子中,GameScoreController
是包装类。
// In build settings look at the Module Identifier.
// This is the one you should use to import swift files from the same target.
#import "AirRivet_Example-Swift.h"
GameScoreController * controller = [[GameScoreController alloc] init];
[controller retrieve:^(NSArray<Foo *> * _Nonnull response) {
NSLog(@"%@", response);
} failure:^(NSError * _Nonnull error) {
NSLog(@"%@", error);
}];
参见“(项目根目录)/Example”
AirRivet 提供了在编写单元测试时模拟数据的方法。
使用 Air
模拟你使用的环境。这可以通过以下 2 步完成
MockEnvironment
。class MockEnvironment<Rivet: EnvironmentConfigurable>: ApplictionEnvironment <Rivet> {
override class shouldMock () {
return true
}
}
class MockFoo: Foo {
override class func environment() -> protocol<Environment, Mockable> {
return MockEnvironment<MockFoo>()
}
override class func transform() -> TransformJSON {
return TransformJSON()
}
}
使用 Core Data 编写单元测试可能比较困难。使用 AirRivet 可以编写针对 CoreData 的单元测试,但仍然较为繁琐。按照上面的步骤操作,但请确保为单元测试提供 managedObjectContext。参见 CoreDataUnitTests
。请确保为您 的 CoreDataUnitTests
实例创建单例。
class MockCoreDataEntity: CoreDataEntity {
override class func environment() -> protocol<Environment, Mockable> {
return MockEnvironment<MockCoreDataEntity>()
}
override class func transform() -> TransformJSON {
return TransformJSON()
}
override class func managedObjectContext() -> NSManagedObjectContext? {
return coredataUnitTests.sharedInstance.managedObjectContext
}
}
在示例项目中,您可以找到使用 Nimbel
编写的示例,这些示例针对 both CoreData 和其他内容。
AirRivet 通过 CocoaPods 和 Swift Package Manager 提供支持。
要使用 CocoaPods 安装它,请将以下行添加到您的 Podfile 中
pod "AirRivet"
不要想得太难,要尽力而为!
有关更多信息,请参阅 贡献指南 wiki 页面。
AirRivet 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。