vendredi 10 juillet 2020

Make function under test to return true or false to activate different integration test scenarios

I have file A that I want to test

let isDbAvailable = async () => {
    return t/f
}

const mainFunction = () => {

    isDbAvailable().then((status)=>{
      if (status){
        //if db is available do this
      } else {
        // do that 
      }
    });

}

exports.mainFunction = () => mainFunction();

In my first integration test I want to test scenario when db is up and running and in second test I want to test scenario where db is not available.

so I have test file

const makeRequest = (resource, count, done) => {
        chai.request(url)
            .put(`/api/myCoolApi/${resource}`)
            .send({ 'something': 'my something' })
            .end(function (err, res) {
                if (res) {
                    if (count < 3) {
                        res.should.have.status(200);
                        res.body.should.eql({ ok: 200 });
                        makeRequest(resource, ++count, done);
                    } else {
                        res.should.have.status(429);
                        res.body.should.eql(string['erroro_err']);
                        done();
                    }
                } else {
                    done(err);
                }
            });
    };

 it('test when db is down', function (done) {
        makeRequest('something', 0, done);
 });

Question. How to make isDbAvailable() to return true in first scenario and false in second?

I was thinking I should be able to import my file A into test

const myModule = rewire('../../src/A')

and then stub it?

it('test when db is down', function (done) {
            const stub = sinon.stub(myModule.__get__("isDbAvailable"), 'create').returns(false);
            makeRequest('something', 0, done);
});

But I guess my syntax is wrong?

I am using chai and sinon for integration test.

Aucun commentaire:

Enregistrer un commentaire