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

How to test is function is called or not? #773

Closed
eMdOS opened this issue Mar 20, 2018 · 7 comments
Closed

How to test is function is called or not? #773

eMdOS opened this issue Mar 20, 2018 · 7 comments

Comments

@eMdOS
Copy link

eMdOS commented Mar 20, 2018

Is there any way to test if a function is called?

When using XCTest framework directly, this can be tested using expectations.

I have seen some code samples using booleans with an initial value as false and the setting them as true when calling the related functions but I don't like this approach.

I wonder if there is an expectation-like option available in Quick.

@shaps80
Copy link
Member

shaps80 commented Mar 20, 2018

A few months ago QuickSpec.current was introduced to expose expectations.

let expectation = QuickSpec.current.expectation(description: "my function was called")
expectation.fulfill()

When testing a closure, this is fairly simple. However if you need to test a function that is called internally, you can do a couple of things.

  1. Subclass and implement the expectation in the mock. This obviously assumes you have a class and its not marked final – less than ideal
  2. Use a protocol in your implementation, then write a mock instance that exposes the expectation – recommended

You can checkout the original PR for this here: #645

@shaps80
Copy link
Member

shaps80 commented Mar 20, 2018

Crude Example:

// protocol definition
protocol NetworkServiceHandling {
    func perform(request: FetchRequest)
}

// concrete instance
final class NetworkService: NetworkServiceHandling { }

// mock instance
final class MockNetworkService: NetworkServiceHandling {
    internal var expectation: XCTestExpectation?

    func perform(request: FetchRequest) {
        expectation?.fulfill()
    }
}

@eMdOS
Copy link
Author

eMdOS commented Mar 21, 2018

I have read some threads about it, but the thing is that I'm getting the message Type 'QuickSpec' has no member 'current' when trying to do it.

I am using carthage and the resolved version is github "Quick/Quick" "v1.2.0".

So, seems like current is not part of the Public API of QuickSpec.

By the way, I am using Swift.

@shaps80
Copy link
Member

shaps80 commented Mar 21, 2018

Ahh... you're right. That API was added in October and the 1.2 release was in September. I know with pods you could just point directly to a tag, branch, etc...

I'm not that familiar with Carthage, but perhaps @ikesyo can help you here.

@eMdOS
Copy link
Author

eMdOS commented Apr 1, 2018

@shaps80 according to the project versioning, the latest released version is the one I am currently using: https://github.com/Quick/Quick/releases/tag/v1.2.0

So, if that API was added in October, why it is not released yet?

Is master branch containing that API?

Thanks in advance.

@shaps80
Copy link
Member

shaps80 commented Apr 1, 2018

Because no release has occurred since v1.2 AFAIK. It does look like it got merged into develop though. So if you point to develop you should be able to get access to that API.

Sent with GitHawk

@eMdOS
Copy link
Author

eMdOS commented Apr 3, 2018

Git it, thanks. I pointed it to master and now it is working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants