-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] Mocking swift Sourcery

, 02 2017 . 18:53 +


ios-, unit- . .



, . , AuthenticationService. , , , , :


protocol AuthenticationService {

    typealias Login = String
    typealias Password = String
    typealias isSucces = Bool

    ///   
    ///
    /// - Parameters:
    ///   - login:  
    ///   - password: 
    /// - Returns:  
    func authenticate(with login: Login, and password: Password) -> isSucces

    ///    
    ///
    /// - Parameters:
    ///   - login:  
    ///   - password: 
    ///   - authenticationHandler: Callback(completionHandler) 
    func asyncAuthenticate(with login: Login, and password: Password, authenticationHandler: @escaping (isSucces) -> Void)

}

viewController, :


class ViewController: UIViewController {

    var authenticationService: AuthenticationService!
    var login = "Login"
    var password = "Password"

    ///  ,    
    var aunthenticationHandler: ((Bool) -> Void) = { (isAuthenticated) in
        print("\n  :")
        isAuthenticated ? print(" ") : print("  ")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        authenticationService = AuthenticationServiceImplementation() // -   ,   , ..    viewController
        performAuthentication()
        performAsyncAuthentication()
    }

    func performAuthentication() {
        let isAuthenticated = authenticationService.authenticate(with: login, and: password)
        print("  :")
        isAuthenticated ? print(" ") : print("  ")
    }

    func performAsyncAuthentication() {
        authenticationService.asyncAuthenticate(with: login, and: password, and: aunthenticationHandler)
    }

}

viewController.



.. , - , viewController'a, . . :


class MockAuthenticationService: AuthenticationService {

    var emulatedResult: Bool? // ,     
    var receivedLogin: AuthenticationService.Login? //     
    var receivedPassword: AuthenticationService.Password? //     
    var receivedAuthenticationHandler: ((AuthenticationService.isSucces) -> Void)? // ,            

    func authenticate(with login: AuthenticationService.Login,
                      and password: AuthenticationService.Password) -> AuthenticationService.isSucces {
        receivedLogin = login
        receivedPassword = password
        return emulatedResult ?? false
    }

    func asyncAuthenticate(with login: AuthenticationService.Login,
                           and password: AuthenticationService.Password,
                           and authenticationHandler: @escaping (AuthenticationService.isSucces) -> Void) {
        receivedLogin = login
        receivedPassword = password
        receivedAuthenticationHandler = authenticationHandler
    }

}

, ( , ). . mockito( android-). , swift read-only ( , , , ). . , . : (ManWithBear).


Sourcery. Sourcery . , AutoMockable.


:


1) pod 'Sourcery'.
2) RunScript .


$PODS_ROOT/Sourcery/bin/sourcery --sources . --templates ./Pods/Sourcery/Templates/AutoMockable.stencil --output ./SwiftMocking

:


"$PODS_ROOT/Sourcery/bin/sourcery" Sourcery.
"--sources ." , ( , ).
"--templates ./Pods/Sourcery/Templates/AutoMockable.stencil" .
"--output ./SwiftMocking" , ( SwiftMocking).


3) AutoMockable.swift :


///    ,    
protocol AutoMockable {}

4) , , AutoMockable. AuthenticationService':


protocol AuthenticationService: AutoMockable {

5) . --ouput, AutoMockable.generated.swift, . .


6) . .


, .


class AuthenticationServiceMock: AuthenticationService {

    //MARK: - authenticate

    var authenticateCalled = false
    var authenticateReceivedArguments: (login: Login, password: Password)?
    var authenticateReturnValue: isSucces!

    func authenticate(with login: Login, and password: Password) -> isSucces {
        authenticateCalled = true
        authenticateReceivedArguments = (login: login, password: password)
        return authenticateReturnValue
    }
    //MARK: - asyncAuthenticate

    var asyncAuthenticateCalled = false
    var asyncAuthenticateReceivedArguments: (login: Login, password: Password, authenticationHandler: (isSucces) -> Void)?

    func asyncAuthenticate(with login: Login, and password: Password, and authenticationHandler: @escaping (isSucces) -> Void) {
        asyncAuthenticateCalled = true
        asyncAuthenticateReceivedArguments = (login: login, password: password, authenticationHandler: authenticationHandler)
    }
}

. :


import XCTest
@testable import SwiftMocking

class SwiftMockingTests: XCTestCase {

    var viewController: ViewController!
    var authenticationService: AuthenticationServiceMock!

    override func setUp() {
        super.setUp()
        authenticationService = AuthenticationServiceMock()
        viewController = ViewController()
        viewController.authenticationService = authenticationService
        viewController.login = "Test login"
        viewController.password = "Test password"
    }

    func testPerformAuthentication() {
        // given
        authenticationService.authenticateReturnValue = true

        // when
        viewController.performAuthentication()

        // then
        XCTAssert(authenticationService.authenticateReceivedArguments?.login == viewController.login, "      ")
        XCTAssert(authenticationService.authenticateReceivedArguments?.password == viewController.password, "      ")
        XCTAssert(authenticationService.authenticateCalled, "    ")
    }

    func testPerformAsyncAuthentication() {
        // given
        var isAuthenticated = false
        viewController.aunthenticationHandler = { isAuthenticated = $0 }

        // when
        viewController.performAsyncAuthentication()
        authenticationService.asyncAuthenticateReceivedArguments?.authenticationHandler(true)

        // then
        XCTAssert(authenticationService.asyncAuthenticateCalled, "     ")
        XCTAssert(authenticationService.asyncAuthenticateReceivedArguments?.login == viewController.login, "       ")
        XCTAssert(authenticationService.asyncAuthenticateReceivedArguments?.password == viewController.password, "       ")
        XCTAssert(isAuthenticated, "    ")
    }

}


Sourcery , . : Equatable ( ).



->
-> Sourcery github
-> sourcery

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332120/

:  

: [1] []
 

:
: 

: ( )

:

  URL