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 stub closure in static function #319

Closed
2 of 4 tasks
terenceLuffy opened this issue Dec 30, 2022 · 1 comment
Closed
2 of 4 tasks

Unable to stub closure in static function #319

terenceLuffy opened this issue Dec 30, 2022 · 1 comment
Labels
framework bug Breaks tests or user testing code

Comments

@terenceLuffy
Copy link

terenceLuffy commented Dec 30, 2022

New Issue Checklist

Overview

When I try to stub closure with will in a static function, it shows error "Unable to infer type of a closure parameter", if I specify the type in the closure parameter, it can be built successfully, but crash when running test.

Example

Source class

class NetworkHelper {
    // MARK: - Network requests
    static func get(
        url: URL,
        token: String?,
        completion: @escaping (Data?, URLResponse?, Error?) -> Void
    ) {
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        // Sample server doesn't need a token as it runs locally
        if let token {
            request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
        }

        URLSession.shared.dataTask(with: request, completionHandler: completion).resume()
    }
}

Test class

func testRequestOtpSuccess() throws {
        let helper = type(of: mock(NetworkHelper.self))
        given(helper.get(url: any(), token: any(), completion: any())).will { completion in //This will show error when building
            completion(nil, nil, nil)
        }
        // The following ways will crash the app
        // error: memory read failed for 0x0
        given(helper.get(url: any(), token: any(), completion: any())).will { (completion: (Data?, URLResponse?, Error?) -> Void) in
            completion(nil, nil, nil)
        }
        given(helper.get(url: any(), token: any(), completion: any())).will {
        }
    }

Expected Behavior

It should be able to stub and run without crashing

Environment

  • Mockingbird CLI version (0.20.0)
  • Xcode and Swift version (swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51) Target: arm64-apple-macosx13.0)
  • Package manager (SPM project)
  • Unit testing framework (XCTest)
  • Custom configuration
    • Mockingbird ignore files
    • Supporting source files
@terenceLuffy terenceLuffy added the framework bug Breaks tests or user testing code label Dec 30, 2022
@terenceLuffy
Copy link
Author

terenceLuffy commented Jan 9, 2023

It works if I do this:
Change static to class

given(helper.get(url: any(), token: any(), completion: any())).will { url, token, completion in
// verify
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework bug Breaks tests or user testing code
Projects
None yet
Development

No branches or pull requests

1 participant