Comments (14)
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.
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.
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.
@kylef any tips on how to debug this?
from mockingjay.
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.
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.
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.
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.
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.
I'm planning on looking into this when I get chance.
from mockingjay.
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.
@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.
I simply use MockingjayProtocol.addStub(matcher: Matcher, builder: Builder)
instead of stub(matcher: Matcher, builder: Builder)
for my test. It worked.
from mockingjay.
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)
- Mockingjay stub method overwriting Cuckoo stub method HOT 2
- Stubbing HTTP redirect
- swift4.2 support HOT 4
- Check if Mockingjay include Mockingjay-Swift.h ! HOT 2
- Pod Swift Version Is Not Specified HOT 1
- URITemplate not updated to swift 5
- Could not find auto-linked framework HOT 5
- Custom matcher runs twice if matcher returns true
- Could not build Objective-C module 'Mockingjay'
- Build with Swift 5 fails if '3.0.0-alpha.1' version is not explicitly specified in the Podfile.
- Unable to use the library on Xcode 13.0 (Swift 5.5)
- The latest 3.0.0-alpha.1 not compiled with Error Ambiguous use of 'tearDown' HOT 2
- Privacy manifest required by Apple
- Matching GET requests with query
- Carthage support
- Add stub after launch HOT 1
- Mockingjay references XCTest HOT 1
- Matcher doesn't match partly URLs HOT 3
- Not working with URLSessionConfiguration.background
- Future of Project HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockingjay.