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
),一个是文本字段是否聚焦,另一个是文本流;所以类似于
可观察的
。根据字符串my
ExampleViewModel
是否聚焦和上下文,my
ExampleViewModel
将向其内部公开的属性发出一个事件,该属性是
可观察的
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()
}
}
}