javascript - TDD principle, how to make the test fail -
i m studying tdd , on, learn examples.
actually m building little (very little) orm introduce me real unit test, stubs , mock.
i run way, played tests , make little implementation (getall method) (fail -> make test pass -> refactor)
actually, here's code :
mongodbadapter
export default class mongoadapter{ getall(){ return new promise((resolve, reject)=>{ resolve(['a','b','c','d']); }); } }
mysqladapter
export default class mysqladapter{ getall(){ return new promise((resolve, reject)=>{ resolve(['a','b','c','d','e']); }); } }
factoryadapter class
import mysqladapter './adapters/mysqladapter'; import mongoadapter './adapters/mongoadapter'; export default class factoryadapter{ static get(name){ if(name.tolowercase() === 'mysql') return new mysqladapter(); if(name.tolowercase() === 'mongodb') return new mongoadapter(); return null; } }
unit tests associated :
testmongoadapter
import factoryadapter '../../app/factoryadapter'; let chai = require('chai'); let sinon = require("sinon"); let expect = chai.expect; /** @test {mongoadapter} */ describe('mongoadapter class',function(){ /** @test {mongoadapter#getall} */ describe('mongoadapter#getall',function(){ it('expect getall() equals [a,b,c,d]',function(done){ let adapter = factoryadapter.get('mongodb'); adapter.getall().then((value)=>{ expect(value).to.deep.equal(['a','b','c','d']); done(); }); }); }); });
and on 2 others. question isn't on syntax way in case.
questions
actually, want use db drivers mysql , mongodb inside of concerned adapter. actually, know implementation should : passing driver inside of constructor method of adapter when creating instance of in factoryadapter.get
my problem point, dont know test should write (that should fail) before implementating code.
actually use pretty much. :d
the database adapter test integration test, not unit test. cannot test in separation database... every test has same structure (according phpunit manual), have fixture, run test on fixture , make assertions. if want test fail need write assertion fails. e.g. have db fixture, contains data, , code should retrieve data. write assertion, requires data code should retrieve database. fails. after can work on code. that's db tests.
if class uses db adapters, have mock out adapter; create fake class, control. after can inject mock adapter instead of real one, , test methods class calls on it. can inject fixtures mock adapter. e.g. set getall returns promise data instead of accessing database , sending queries data.
i see use sinon. here docs mocks http://sinonjs.org/docs/#mocks-api .
Comments
Post a Comment