kylef / Mockingjay

An elegant library for stubbing HTTP requests with ease in Swift
BSD 3-Clause "New" or "Revised" License
1.49k stars 178 forks source link

Add stub after launch #94

Open shunmugarajrntbci opened 6 years ago

shunmugarajrntbci commented 6 years ago

Is it possible to add new stub after launch, after navigating few viewcontroller. I need to add new stub at 3rd screen replacing old stub

kylef commented 6 years ago

Yes, you can add stubs whenever you like. The one limitation is that you need to ensure that the NSURLSessionConfiguration includes MockingjayProtocol when the NSURLSession instance is created.

If you are using the default session and would like the configuration swizzled to contain mockingjay's session (default behaviour when you run Mockingjay in tests). Then you can call mockingjaySwizzleDefaultSessionConfiguration upfront (during app launch or such, providing it is before you create any NSURLSession that will performing the requests):

URLSessionConfiguration.mockingjaySwizzleDefaultSessionConfiguration()

Alternatively, when creating NSURLSession, you can add MockingjayProtocol to the list of protocol classes at the front:

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

let session = URLSession(configuration: configuration)

let task = session.dataWithRequest(...)