Mvvm 用RxSwift测试ViewModel
我在当前工作的项目中使用Mvvm 用RxSwift测试ViewModel,mvvm,swift2,rx-swift,Mvvm,Swift2,Rx Swift,我在当前工作的项目中使用modelviewmodel,并使用RxSwift,RxBlocking和RxTests。目前,我正在尝试测试ViewModel,但在解决这个问题时遇到了很多麻烦 假设我的ExampleViewController有一个ExampleViewModel。我的ExampleViewModel需要一个可观察的流,它是来自UITextField的两个流的组合(combinelatetest),一个是文本字段是否聚焦,另一个是文本流;所以类似于可观察的。根据字符串myExampl
modelviewmodel
,并使用RxSwift
,RxBlocking
和RxTests
。目前,我正在尝试测试ViewModel,但在解决这个问题时遇到了很多麻烦
假设我的ExampleViewController
有一个ExampleViewModel
。我的ExampleViewModel
需要一个可观察的流,它是来自UITextField
的两个流的组合(combinelatetest
),一个是文本字段是否聚焦,另一个是文本流;所以类似于可观察的
。根据字符串myExampleViewModel
是否聚焦和上下文,myExampleViewModel
将向其内部公开的属性发出一个事件,该属性是可观察的
UITextField的背景色状态<代码>可观察的
ExampleViewModel.swift
:
class ExampleViewModel {
private let disposeBag = DisposeBag()
private let _textFieldColor: PublishSubject<UIColor>
var textFieldColor: Observable<UIColor> { get { return self._textFieldColor.asObservable() } }
init(textFieldObservable: Observable<(Bool, String)>) {
textFieldObservable.subscribeNext { (focus, text) in
self.validateTextField(focus, text: text)
}.addDisposableTo(self.disposeBag)
}
func validateTextField(focus: Bool, text: String) {
if !focus && !text.isEmpty {
self._textFieldColor.onNext(UIColor.whiteColor())
} else {
self._textFieldColor.onNext(UIColor.redColor())
}
}
}
class ExampleViewModel{
私有出租dispebag=dispebag()
private let\u textFieldColor:PublishSubject
var textFieldColor:Observable{get{return self.\u textFieldColor.asObservable()}
init(textFieldObservable:Observable){
textFieldObservable.subscribeNext{(焦点,文本)在
self.validateTextField(焦点,文本:文本)
}.addDisposableTo(self.disposeBag)
}
func validateTextField(焦点:Bool,文本:字符串){
if!focus&!text.isEmpty{
self.\u textFieldColor.onNext(UIColor.whiteColor())
}否则{
self.\u textFieldColor.onNext(UIColor.redColor())
}
}
}
(对不起,我不知道如何正确格式化)
基本上,我想测试ExampleViewModel
类,并通过控制焦点和文本输入来测试它是否发出正确的UIColor
谢谢谢谢我同事的建议,我找到了一种更好的方法来构建可测试性的ExampleViewModel
。通过使用ExampleViewModel
分离验证方法,并使用使用验证器的map
操作符设置textFieldColor
可观察的
,验证在外部完成,而不使用Rx
简化逻辑测试
示例视图模型
class ExampleViewModel {
var textFieldColor: Observable<UIColor>
init(
textFieldText: Observable<String>,
textFieldFocus: Observable<Bool>,
validator: TextFieldValidator
) {
self. textFieldColor = Observable.combineLatest(textFieldText, textFieldFocus) { ($0, $1) }. map { validator.validate($1, text: $0) }
}
}
class TextFieldValidator {
func validate(focus: Bool, text: String) -> UIColor {
if !focus && !text.isEmpty {
return UIColor.whiteColor()
} else {
return UIColor.redColor()
}
}
}
class ExampleViewModel{
var textFieldColor:可观察
初始化(
textFieldText:可观察,
textFieldFocus:可观察,
验证程序:TextFieldValidator
) {
self.textFieldColor=Observable.CombineTest(textFieldText,textFieldFocus){($0,$1)}.map{validator.validate($1,text:$0)}
}
}
类TextFieldValidator{
func验证(焦点:Bool,文本:String)->UIColor{
if!focus&!text.isEmpty{
返回UIColor.whiteColor()
}否则{
返回UIColor.redColor()
}
}
}
多亏了同事的建议,我找到了一种更好的方法来构建可测试性的示例视图模型。通过使用ExampleViewModel
分离验证方法,并使用使用验证器的map
操作符设置textFieldColor
可观察的
,验证在外部完成,而不使用Rx
简化逻辑测试
示例视图模型
class ExampleViewModel {
var textFieldColor: Observable<UIColor>
init(
textFieldText: Observable<String>,
textFieldFocus: Observable<Bool>,
validator: TextFieldValidator
) {
self. textFieldColor = Observable.combineLatest(textFieldText, textFieldFocus) { ($0, $1) }. map { validator.validate($1, text: $0) }
}
}
class TextFieldValidator {
func validate(focus: Bool, text: String) -> UIColor {
if !focus && !text.isEmpty {
return UIColor.whiteColor()
} else {
return UIColor.redColor()
}
}
}
class ExampleViewModel{
var textFieldColor:可观察
初始化(
textFieldText:可观察,
textFieldFocus:可观察,
验证程序:TextFieldValidator
) {
self.textFieldColor=Observable.CombineTest(textFieldText,textFieldFocus){($0,$1)}.map{validator.validate($1,text:$0)}
}
}
类TextFieldValidator{
func验证(焦点:Bool,文本:String)->UIColor{
if!focus&!text.isEmpty{
返回UIColor.whiteColor()
}否则{
返回UIColor.redColor()
}
}
}