unit testing - Handling several event listeners -


update: here's fiddle of problem. tests pass once, , fail next time:

http://jsfiddle.net/samselikoff/hhk6u/4/

the problem departments has events.on("userset:company"), both variables respond event.


this general question unit testing. in app, event fired, , several other pieces of app listen event. i'd unit test each piece separately, since performing different functions; this, have fire off event in each test.

this causes problems, since first test must fire off event, triggering listeners in other tests. how can keep tests atomic while still testing multiple event listeners?

(i using qunit, think more general unit-testing question).

answer:

jeferson correct. 1 easy way solve this, use events.once instead of events.on. way clean events each test.

all calls async methods should tested using "asynctest" methods , making sure wrap calls in other functions calls qunit.start() when assertions data ready collected , analyzed.

i updated jsfiddle working code: http://jsfiddle.net/hhk6u/8/ new code is:

qunit.config.autostart = false; qunit.config.testtimeout = 1000;  asynctest('some test needs companies.', function() {     function getcompanies() {         var companies = new companies();         ok(1);         start();     }     settimeout(getcompanies, 500); });  asynctest('some other async test triggers listener in companies.', function() {        var companies = new companies();      events.trigger("userset:company", { name: "acme", id: 1 });      stop();     events.on('fetched:departments', function(response) {         console.log(response);         deepequal(response, [1, 2, 3]);         start();     }); }); 

see answer in other question more details: test fails succeeds

hope helps you!


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -