Giter Site home page Giter Site logo

stubOnce behavior / documentation about moxios HOT 3 CLOSED

axios avatar axios commented on April 28, 2024 4
stubOnce behavior / documentation

from moxios.

Comments (3)

nicosommi avatar nicosommi commented on April 28, 2024 2

As I want to test a retry mechanism and the error handling regarding that, I also found it confusing since I thought that I was covered with that method.

Anyways, if anyone interested, you can do this by actually manipulating the stubs array right on the main moxios instance.
E.g.

        moxios.stubOnce(
          'put',
          'http://myapi/products/1',
          {}
        )

        let obj = moxios.stubs.mostRecent()
        let count = 0
        Object.defineProperty(obj, 'response', {
          get: () => {
            count++
            if (count > 1) {
              return { status: 200, response: '' }
            }
            return { status: 500, response: '' }
          }
        })

Or maybe is a better way already to do this.
Otherwise I can prepare a PR for this if maintainers are interested @mzabriskie

from moxios.

anilanar avatar anilanar commented on April 28, 2024

Kamino cloned this issue to anilanar/moxios

from moxios.

mrjacobbloom avatar mrjacobbloom commented on April 28, 2024

I assumed this function would let me temporarily override an existing stub in the middle of our test suite. Here was what I did instead, in case it helps anyone else:

function overrideStub(url, response) {
    const l = moxios.stubs.count();
    for (let i = 0; i < l; i++) {
        const stub = moxios.stubs.at(i)
        if (stub.url === url) {
            const oldResponse = stub.response;
            const restoreFunc = (() => stub.response = oldResponse);

            stub.response = response;
            return restoreFunc;
        }
    }
}

const restoreStub = overrideStub("../api/foo", {
    status: 500,
    response: 'An error occurred : Bar baz.'
});

buttonThatExpectsError.click();

moxios.wait(() => {
    assert(....);
    restoreStub();
})

from moxios.

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.