luisobo / Nocilla

Testing HTTP requests has never been easier. Nocilla: Stunning HTTP stubbing for iOS and Mac OS X.
https://twitter.com/luisobo
MIT License
1.84k stars 172 forks source link

LSNocilla.sharedInstance().stop() Not working #136

Open fcruzsbtv opened 8 years ago

fcruzsbtv commented 8 years ago

I have a multiple test set, one of them uses nocilla so I execute LSNocilla.sharedInstance().start() when it begin and stop it at the end, but when the other test that needs actual internet runs I keep getting "NocillaUnexpectedRequest', reason: 'An unexpected HTTP request was fired.'"

andrewkboyd commented 8 years ago

I don't know what you mean by 'multiple test set'

Please provide sample code that reproduces the issue/more detail. I don't know how to look into it based on the info you have provided.

stepanhruda commented 8 years ago

Just ran into and debugged this issue. The problem happens if you create a new URL session when Nocilla is running, then stop Nocilla and continue using the session.

Nocilla swizzles the URL protocols on the default configuration – it cleans them up from the default configuration, but when creating a new session they are likely transferred to the session object, so any session objects created continue intercepting requests.

https://github.com/luisobo/Nocilla/blob/master/Nocilla/Hooks/NSURLSession/LSNSURLSessionHook.m

nmarkovic04 commented 8 years ago

Yep, just ran into this problem that @stepanhruda described.

  1. Test runs fine, expectation is fulfilled.
  2. In -tearDown, LSNocilla.sharedInstance().stop() is called.
  3. The request that is called as a result of the stubbed request gets intercepted after .calling .stop().

e.g. We stub /user/login to return 200 OK. Works fine, but sync data gets called as a result of successful login. We can stub that request too, but it makes no difference since it gets called after .stop(). In other words, at the moment when - (LSStubResponse *)responseForRequest:(id<LSHTTPRequest>)actualRequest { is called, (lldb) po self.started returns NO, and self.requests has 0 elements so it will always throw the exception.

rohan-panchal commented 8 years ago

I'm experiencing this too.

Right now i just fixed it by using an instance of the network client rather than the shared singleton.

But reproduction steps are the same.

Wooder commented 7 years ago

I'm experiencing this too.

I "fixed" it with this workaround (calling the stop-function in the setUp()-functions code):

    override func setUp() {
        super.setUp()
        if (LSNocilla.sharedInstance().isStarted) {
            LSNocilla.sharedInstance().stop()
        }
        LSNocilla.sharedInstance().start()
        ...
    }

    override func tearDown() {
        //LSNocilla.sharedInstance().stop() // causes "NocillaUnexpectedRequest', reason: 'An unexpected HTTP request was fired.'"
        super.tearDown()
    }