forked from sergdort/CleanArchitectureRxSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sergdort#20 from vadimue/feature/postsTests
Add tests for PostsViewModel
- Loading branch information
Showing
5 changed files
with
217 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
CleanArchitectureRxSwiftTests/CleanArchitectureRxSwiftTests.swift
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
CleanArchitectureRxSwiftTests/Scenes/AllPosts/AllPostsUseCaseMock.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@testable import CleanArchitectureRxSwift | ||
import RxSwift | ||
import Domain | ||
|
||
class AllPostsUseCaseMock: Domain.AllPostsUseCase { | ||
|
||
var posts_ReturnValue: Observable<[Post]> = Observable.just([]) | ||
var posts_Called = false | ||
|
||
func posts() -> Observable<[Post]> { | ||
posts_Called = true | ||
return posts_ReturnValue | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
CleanArchitectureRxSwiftTests/Scenes/AllPosts/PostsNavigatorMock.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@testable import CleanArchitectureRxSwift | ||
import Domain | ||
import RxSwift | ||
|
||
class PostNavigatorMock: PostsNavigator { | ||
|
||
var toPosts_Called = false | ||
|
||
func toPosts() { | ||
toPosts_Called = true | ||
} | ||
|
||
var toCreatePost_Called = false | ||
|
||
func toCreatePost() { | ||
toCreatePost_Called = true | ||
} | ||
|
||
var toPost_post_Called = false | ||
var toPost_post_ReceivedArguments: Post? | ||
|
||
func toPost(_ post: Post) { | ||
toPost_post_Called = true | ||
toPost_post_ReceivedArguments = post | ||
} | ||
|
||
} |
144 changes: 144 additions & 0 deletions
144
CleanArchitectureRxSwiftTests/Scenes/AllPosts/PostsViewModelTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
@testable import CleanArchitectureRxSwift | ||
import Domain | ||
import XCTest | ||
import RxSwift | ||
import RxCocoa | ||
import RxBlocking | ||
|
||
enum TestError: Error { | ||
case test | ||
} | ||
|
||
class PostsViewModelTests: XCTestCase { | ||
|
||
var allPostUseCase: AllPostsUseCaseMock! | ||
var postsNavigator: PostNavigatorMock! | ||
var viewModel: PostsViewModel! | ||
|
||
let disposeBag = DisposeBag() | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
allPostUseCase = AllPostsUseCaseMock() | ||
postsNavigator = PostNavigatorMock() | ||
|
||
viewModel = PostsViewModel(useCase: allPostUseCase, | ||
navigator: postsNavigator) | ||
} | ||
|
||
func test_transform_triggerInvoked_postEmited() { | ||
// arrange | ||
let trigger = PublishSubject<Void>() | ||
let input = createInput(trigger: trigger) | ||
let output = viewModel.transform(input: input) | ||
|
||
// act | ||
output.posts.drive().disposed(by: disposeBag) | ||
trigger.onNext() | ||
|
||
// assert | ||
XCTAssert(allPostUseCase.posts_Called) | ||
} | ||
|
||
|
||
func test_transform_sendPost_trackFetching() { | ||
// arrange | ||
let trigger = PublishSubject<Void>() | ||
let output = viewModel.transform(input: createInput(trigger: trigger)) | ||
let expectedFetching = [true, false] | ||
var actualFetching: [Bool] = [] | ||
|
||
// act | ||
output.fetching | ||
.do(onNext: { actualFetching.append($0) }, | ||
onSubscribe: { actualFetching.append(true) }) | ||
.drive() | ||
.disposed(by: disposeBag) | ||
trigger.onNext() | ||
|
||
// assert | ||
XCTAssertEqual(actualFetching, expectedFetching) | ||
} | ||
|
||
func test_transform_postEmitError_trackError() { | ||
// arrange | ||
let trigger = PublishSubject<Void>() | ||
let output = viewModel.transform(input: createInput(trigger: trigger)) | ||
allPostUseCase.posts_ReturnValue = Observable.error(TestError.test) | ||
|
||
// act | ||
output.posts.drive().disposed(by: disposeBag) | ||
output.error.drive().disposed(by: disposeBag) | ||
trigger.onNext() | ||
let error = try! output.error.toBlocking().first() | ||
|
||
// assert | ||
XCTAssertNotNil(error) | ||
} | ||
|
||
func test_transform_triggerInvoked_mapPostsToViewModels() { | ||
// arrange | ||
let trigger = PublishSubject<Void>() | ||
let output = viewModel.transform(input: createInput(trigger: trigger)) | ||
allPostUseCase.posts_ReturnValue = Observable.just(createPosts()) | ||
|
||
// act | ||
output.posts.drive().disposed(by: disposeBag) | ||
trigger.onNext() | ||
let posts = try! output.posts.toBlocking().first()! | ||
|
||
// assert | ||
XCTAssertEqual(posts.count, 2) | ||
} | ||
|
||
func test_transform_selectedPostInvoked_navigateToPost() { | ||
// arrange | ||
let select = PublishSubject<IndexPath>() | ||
let output = viewModel.transform(input: createInput(selection: select)) | ||
let posts = createPosts() | ||
allPostUseCase.posts_ReturnValue = Observable.just(posts) | ||
|
||
// act | ||
output.posts.drive().disposed(by: disposeBag) | ||
output.selectedPost.drive().disposed(by: disposeBag) | ||
select.onNext(IndexPath(row: 1, section: 0)) | ||
|
||
// assert | ||
XCTAssertTrue(postsNavigator.toPost_post_Called) | ||
XCTAssertEqual(postsNavigator.toPost_post_ReceivedArguments, posts[1]) | ||
} | ||
|
||
func test_transform_createPostInvoked_navigateToCreatePost() { | ||
// arrange | ||
let create = PublishSubject<Void>() | ||
let output = viewModel.transform(input: createInput(createPostTrigger: create)) | ||
let posts = createPosts() | ||
allPostUseCase.posts_ReturnValue = Observable.just(posts) | ||
|
||
// act | ||
output.posts.drive().disposed(by: disposeBag) | ||
output.createPost.drive().disposed(by: disposeBag) | ||
create.onNext() | ||
|
||
// assert | ||
XCTAssertTrue(postsNavigator.toCreatePost_Called) | ||
} | ||
|
||
private func createInput(trigger: Observable<Void> = Observable.just(), | ||
createPostTrigger: Observable<Void> = Observable.never(), | ||
selection: Observable<IndexPath> = Observable.never()) | ||
-> PostsViewModel.Input { | ||
return PostsViewModel.Input( | ||
trigger: trigger.asDriverOnErrorJustComplete(), | ||
createPostTrigger: createPostTrigger.asDriverOnErrorJustComplete(), | ||
selection: selection.asDriverOnErrorJustComplete()) | ||
} | ||
|
||
private func createPosts() -> [Post] { | ||
return [ | ||
Post(body: "body 1", title: "title 1", uid: "uid 1", userId: "userId 1"), | ||
Post(body: "body 2", title: "title 2", uid: "uid 2", userId: "userId 2") | ||
] | ||
} | ||
} |