javascript - Refactoring some Unit Test that have lots of repeated code. -


i working on writing some tests function wrote: current code works expect need dry code , refactor. below see unit tests i've writtern:

qunit.test('localized date in honolulu', assert => {    const stub = sinon.stub(constantdate, 'gettimezoneoffset', () => {      return '600';    });    console.log('timeset', timeset());    assert.strictequal(timeset(), '2017-07-29t14:00:00.000z', 'there needs message here');    stub.restore();  });    qunit.test('san francisco date , time', assert => {    const stub = sinon.stub(constantdate, 'gettimezoneoffset', () => {      return '420';    });    assert.strictequal(timeset(), '2017-07-29t17:00:00.000z');    stub.restore();  });    qunit.test('sydney time', assert => {    const stub = sinon.stub(constantdate, 'gettimezoneoffset', () => {      return '-600';    });    assert.strictequal(timeset(), '2017-07-30t10:00:00.000z', 'expected time in sydney 10am');    stub.restore();  });

although seems me should able refactor stub i'm finding challenging because every stub has different return value every time. can please suggestions how can make code clean , dry.

one suggestion use partial functions. you're aware, first 2 parameters in sinon.stub same every unit test. so, before unit tests execute can create function

const timezoneoffsetstub = (callback) => {    return sinon.stub(constantdate, 'gettimezoneoffset', callback); } 

(this assumes constantdate variable defined globally) stub in each unit test, have define different callbacks are.

qunit.test('localized date in honolulu', assert => {   const stub = timezoneoffsetstub(() => '600');   ... });  qunit.test('san francisco date , time', assert => {   const stub = timezoneoffsetstub(() => '420');   ... }); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -