GithubHelp home page GithubHelp logo

Problem using MockingJay about mockingjay HOT 14 CLOSED

kylef avatar kylef commented on September 1, 2024
Problem using MockingJay

from mockingjay.

Comments (14)

kylef avatar kylef commented on September 1, 2024 1

I've made some changes in Mockingjay 1.2.1 which now swizzles NSURLSessionConfiguration on load of Mockingjay.

This fixes problems when you create a session and then register a stub before we've added the Mockingjay protocol to NSURLSessionConfiguration.

If anyone is still having any problems with Mockingjay after 1.2.1, please file a new issue with steps to reproduce the problem and I'll take a look.

Thanks all!

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

I'm seeing a similar thing with Xcode 7.2 and Swift 2.1.1 using the same technique as a different project that is known to work with Xcode 7.0 and Swift 2.0

I'll try to provide more details.

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

From what I can tell, differences in Xcode / Swift versions have no relevance to my issue.

I've tried stepping through in the debugger but my matcher is never called.

This is the relevant spec and test failure.

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

@kylef any tips on how to debug this?

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

I little bit more info – my APIClient accepts a configuration argument, so I created a configuration the same as the one that Mockingly swizzles with and passed that in:

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.protocolClasses = [MockingjayProtocol.self] as [AnyClass] + configuration.protocolClasses!

let token = "token"
let client = APIClient(token: token, configuration: configuration)

This produced the same test failure.

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

After setting a breakpoint in canInitWithRequest: on MockingjayProtocol I'm able to step through and see that my matcher is called.

This now looks like an error on my part. Why I wasn't able to step through or pause execution on those breakpoints before, I'm not sure.

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

I fixed the issue on my side and the test passed, so I removed the manually passed configuration and can confirm that canInitWithRequest: is never called.

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

I can only assume that method_exchangeImplementations(defaultSessionConfiguration, mockingjayDefaultSessionConfiguration) is not working as expected, since that line is executed but mockingjayDefaultSessionConfiguration() is never called.

from mockingjay.

kylef avatar kylef commented on September 1, 2024

Would you be able to check protocolClasses in NSURLSessionConfiguration inside NSURLSession returns an array of protocols with MockingjayProtocol being the first result?

This should only happen after the first stub was registered via XCTestCase.stub, I'm wondering if NSURLSession makes a copy of the protocols before this is registered.

If this isn't the case, perhaps you could try explicitly exchanging these methods by calling the following before anything related to NSURLSession happens:

NSURLSessionConfiguration.mockingjaySwizzleDefaultSessionConfiguration()

If this solves the problem, then we will need to revisit where this exchange is done within Mockingjay.

from mockingjay.

paulyoung avatar paulyoung commented on September 1, 2024

I'm planning on looking into this when I get chance.

from mockingjay.

tamamachi avatar tamamachi commented on September 1, 2024

I think I encountered the same problem when I updated Mockingjay form 1.1.0 to 1.1.1.
I'm using Quick and adding NSURLSessionConfiguration.mockingjaySwizzleDefaultSessionConfiguration() to beforeSuite worked like a charm!
Thanks a lot.

from mockingjay.

krodak avatar krodak commented on September 1, 2024

@kylef I can confirm too that currently, method swizzling doesn't work.
I tried @paulyoung approach but to no avail.
Even if I put swizzling in AppDelegate it won't replace the default session.

from mockingjay.

ffrddrll avatar ffrddrll commented on September 1, 2024

I simply use MockingjayProtocol.addStub(matcher: Matcher, builder: Builder) instead of stub(matcher: Matcher, builder: Builder) for my test. It worked.

from mockingjay.

IvanRublev avatar IvanRublev commented on September 1, 2024

Swizzling works for Xcode 7.2 and Swift 2.1.1. And stubbing goes well.
Check the order in which stubbing (and as consequence swizzling) and creation of NSURLSession instance occurs.

For example, if you use the Alamofire and your tested target is calling Alamofire.Manager.sharedInstance somewhere on the app's start, then this sharedInstance's underlying NSURLSession object's configuration is not swizzeled with Mockingjay protocol. It's because the sharedInstance is initialized on Manager class load. And that occurs before your test starts and swizzling occurs.)

To workaround this you should redesign your app or test to use the custom Manager instance. If you make Mockingjay to swizzle before initializing the Manager instance:

NSURLSessionConfiguration.mockingjaySwizzleDefaultSessionConfiguration()
let requestManager = Manager(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

Then requests to the requestManager will be stubbed successfully.

from mockingjay.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.