Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get started with Mocking using MockSwift. #128

Closed
azamsharp opened this issue Dec 16, 2022 · 1 comment
Closed

Unable to get started with Mocking using MockSwift. #128

azamsharp opened this issue Dec 16, 2022 · 1 comment
Labels
question Further information is requested

Comments

@azamsharp
Copy link

I created a very simple protocol which I want to mock.

protocol CalculatorServiceProtocol {
    func add(a: Int, b: Int) -> Int
}

In my test I write the following:

final class when_user_performs_add_operation_using_calculator: XCTestCase {

    @Mock private var calculatorService: CalculatorServiceProtocol
    
    func test_should_add_successfully() {
       
        // expectation
        given(calculatorService).??? // how to call the add method and return value
        
        }
}

My confusion is that how do I call the add method on the CalculatorServiceProtocol. The .add method never shows up.

@azamsharp azamsharp added the question Further information is requested label Dec 16, 2022
@leoture
Copy link
Owner

leoture commented Dec 17, 2022

Hello!

You need 3 extensions (Mock, Given, Then) as described here https://github.com/leoture/MockSwift#write-mocks.
I suggest you to use Sourcery to generate them.

Result for CalculatorServiceProtocol:

extension Mock: CalculatorServiceProtocol where WrappedType == CalculatorServiceProtocol {
  public func add(a: Int, b: Int) -> Int {
    mocked(a, b)
  }
}

extension Given where WrappedType == CalculatorServiceProtocol {
  public func add(a: Predicate<Int>, b: Predicate<Int>) -> Mockable<Int> {
    mockable(a, b)
  }
  public func add(a: Int, b: Int) -> Mockable<Int> {
    mockable(a, b)
  }
}

extension Then where WrappedType == CalculatorServiceProtocol {
  public func add(a: Predicate<Int>, b: Predicate<Int>) -> Verifiable<Int> {
    verifiable(a, b)
  }
  public func add(a: Int, b: Int) -> Verifiable<Int> {
    verifiable(a, b)
  }
}

@leoture leoture closed this as completed Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants