ember.js - Unit test a controller action that makes calls to async relationships -
i have 2 models, program
, project
. program
has many project
s.
app.program = ds.model.extend projects: ds.hasmany 'project', async: true app.project = ds.model.extend program: ds.belongsto 'program'
i have arraycontroller
responsible displaying projects in program. each project rendered has destroy
link (a simple action on arraycontroller
).
app.projectsindexcontroller = ember.arraycontroller.extend needs: 'program' program: ember.computed.alias 'controllers.program.model' actions: destroy: (project) -> @get('program.projects').then (projects) -> projects.removeobject(project) # how can test line? @get('program').save() project.destroyrecord()
since relationships async, calling program.get('projects')
returns promise.
i'm using firebase (and emberfire
) backend, stores relationships like
programs: { programid: { projects: { projectid: true } } } projects: { program: 'programid' }
i'm using ember-qunit
, sinon
stub/mock library. first attempt @ testing makes heavy use of sinon.spy()
.
modulefor 'controller:projects.index', 'unit - controller - projects index', needs: ['controller:program'] test 'actions - destroy', -> ctrl = @subject() programctrl = ctrl.get('controllers.program') project1 = em.object.create(title: 'project #1', destroyrecord: sinon.spy()) project2 = em.object.create(title: 'project #2', destroyrecord: sinon.spy()) projects = em.object.create(removeobject: sinon.spy()) program = em.object.create title: 'program #1' projects: then: sinon.stub().yields(projects) save: sinon.spy() ember.run -> programctrl.set 'model', program ctrl.send 'destroy', project1 ok(program.projects.then.calledonce, 'removes project program') ok(program.save.calledonce, 'saves program') ok(project1.destroyrecord.calledonce, 'destroys project')
i'm creating new ember objects, since there no clear way me instantiate instance of models in tests (at least know about). every function gets called in action uses sinon.spy()
can make assertions did in fact called.
coming rails background seems lot of test code 4 relatively simple lines of coffeescript, leads me believe might going wrong way.
either way, overall question this:
how can test (using sinon, or other way) removeobject()
in fact called on program.projects
in async callback function?
also, there easier way me stub models without creating new ember objects in every test, in such way can make assertions above?
Comments
Post a Comment