Giter Site home page Giter Site logo

Comments (5)

tcamin avatar tcamin commented on June 15, 2024

There is a test case that covers this scenario. Stubbing request are evaluated with LIFO order so it should work as expected. I would double check that the url that is executed matches the second stub.

from sbtuitesttunnel.

mfernandez94 avatar mfernandez94 commented on June 15, 2024

There is a test case that covers this scenario. Stubbing request are evaluated with LIFO order so it should work as expected. I would double check that the url that is executed matches the second stub.

I want the opposite to happen, I don't want the URLs to be considered the same, I'd like to treat myhost.com/v1/user/1234/contact as a different URL than myhost.com/v1/user since in the test runs they are called multiple times themselves, some type of exact match where it doesn't just match on the base url but the whole path

from sbtuitesttunnel.

tcamin avatar tcamin commented on June 15, 2024

I think we're stating the same: after setting

app.stubRequests(matching: SBTRequestMatch.init(url: "myhost.com/v1/user/1234/contact"), 
                 response: SBTStubResponse(fileNamed: "user_contact.json"))

requests to myhost.com/v1/user/1234/contact should return the "user_contact.json" stub and all other requests to myhost.com/v1/user will return the "user.json" one.

Could you add a test case to cover specifically your case? Would be easier to debug if there's actually a problem.

from sbtuitesttunnel.

mfernandez94 avatar mfernandez94 commented on June 15, 2024

I think we're stating the same: after setting

app.stubRequests(matching: SBTRequestMatch.init(url: "myhost.com/v1/user/1234/contact"), 
                 response: SBTStubResponse(fileNamed: "user_contact.json"))

requests to myhost.com/v1/user/1234/contact should return the "user_contact.json" stub and all other requests to myhost.com/v1/user will return the "user.json" one.

Could you add a test case to cover specifically your case? Would be easier to debug if there's actually a problem.

Yeah definitely, so if I write the tests like this with stubs in this order, both tests pass

    func testURLPaths() {
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user"), response: SBTStubResponse(response: ["stubbed": 1]))
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user/100/contact"), response: SBTStubResponse(response: ["stubbed": 2]))

        let result1 = request.dataTaskNetwork(urlString: "https://httpbin.org/user")
        XCTAssertTrue(request.isStubbed(result1, expectedStubValue: 1))
        let result2 = request.dataTaskNetwork(urlString: "httpbin.org/user/100/contact")
        XCTAssertTrue(request.isStubbed(result2, expectedStubValue: 2))
        XCTAssert(app.stubRequestsRemoveAll())
    }

When you swap the stub order, the second test now fails because its returning 1 instead of the expected value 2

    func testURLPaths() {
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user/100/contact"), response: SBTStubResponse(response: ["stubbed": 2]))
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user"), response: SBTStubResponse(response: ["stubbed": 1]))

        let result1 = request.dataTaskNetwork(urlString: "https://httpbin.org/user")
        XCTAssertTrue(request.isStubbed(result1, expectedStubValue: 1))
        let result2 = request.dataTaskNetwork(urlString: "httpbin.org/user/100/contact")
        XCTAssertTrue(request.isStubbed(result2, expectedStubValue: 2))
        XCTAssert(app.stubRequestsRemoveAll())
    }

In the above scenario is that what is expected? If so is there a way to get what I want to work?

from sbtuitesttunnel.

tcamin avatar tcamin commented on June 15, 2024

As mentioned stubRequests are evaluated in LIFO order so the above failure is the expected behaviour.

SBTRequestMatch accepts regular expressions so you might want to modify the second stub so that it doesn't match against /contact.

app.stubRequests(matching: SBTRequestMatch(url: #"(?!.*\/contact)httpbin.org/user"#), response: SBTStubResponse(response: ["stubbed": 1]))
app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user/100/contact"), response: SBTStubResponse(response: ["stubbed": 2]))

from sbtuitesttunnel.

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.