Advertisement
Guest User

Test Delegate

a guest
Jan 24th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.79 KB | None | 0 0
  1. import XCTest
  2.  
  3. class MyViewModel {
  4.     weak var delegate: MyDelegateProtocol?
  5.     func viewModelFunc() {
  6.         delegate?.myFunc()
  7.     }
  8. }
  9.  
  10. /// Protocol defined in our app.
  11. protocol MyDelegateProtocol: class {
  12.     func myFunc()
  13. }
  14.  
  15. /// Class, implementing our protocol, defined in the test case.
  16. class TestMyDelegate: MyDelegateProtocol {
  17.     var myFuncHelper: Bool = false
  18.     func myFunc() {
  19.         myFuncHelper = true
  20.     }
  21. }
  22.  
  23. /// ViewModel we want to test.
  24. class TestMyViewModel: XCTestCase {
  25.     func testDelegate() {
  26.         let viewModel = MyViewModel()
  27.         let delegate = TestMyDelegate()
  28.         viewModel.delegate = delegate
  29.        
  30.         XCTAssertFalse(delegate.myFuncHelper)
  31.         viewModel.viewModelFunc()
  32.         XCTAssertTrue(delegate.myFuncHelper)
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement