Skip to content

Instantly share code, notes, and snippets.

@lucasdavila
Last active August 29, 2015 14:21
Show Gist options
  • Save lucasdavila/385e3b12175a0934569d to your computer and use it in GitHub Desktop.
Save lucasdavila/385e3b12175a0934569d to your computer and use it in GitHub Desktop.
Backbone.js router testing
/*global define*/
define([
'routes/base',
'jquery',
'views/group_users'
], function (BaseRouter, $, GroupUsersView) {
'use strict';
var GroupUserRouter = BaseRouter.extend({
routes: {
'groups' : 'index'
},
index: function() {
var view = new GroupUsersView(),
container = $('#app-container');
container.html(view.render().$el);
}
});
return GroupUserRouter;
});
/*global define, describe, it, expect */
define([
'routes/group_user'
],function(Router) {
'use strict';
describe('GroupUserRouter', function() {
var subject;
beforeEach(function() {
subject = new Router();
});
describe('#routes', function(){
it('routes groups to index', function() {
expect(subject.routes.groups).to.be.eq('index');
});
});
describe('#index', function() {
beforeEach(function() {
// levando em consideração que não é possivel adicionar um spy (stub) para window.foo
// como nos exemplos abaixo, pois o projeto usa require.js para carregar dependencias.
//
// this.viewStub = sinon.stub(window, 'GroupUsersView').returns(new Backbone.View());
// this.jqueryStub = sinon.stub(window, '$');
// duvida 1: como "stubar" o resultado do metodo render de uma instância da classe GroupUsersView?
// duvida 2: como "stubar" um elemento jquery com seletor "#app-container"?
});
it('renders the resources view', function() {
subject.index();
// duvida 3: como testar que stud do elemento jquery,
// teve o metodo ".html" chamado com o "(stub da instância da view).$el"?
});
});
});
});
@lucasdavila
Copy link
Author

@renancarvalho, a tua sugestão funcionou (abaixo segue código funcional), isto permite que eu teste se a view foi inicializada, e tambem se outros metodos foram chamados.

Mas isto não faz exatamente o que eu queria, que seria testar se o elemento jquery, teve o metodo .html chamado com o $el da view como argumento.

Fiz algumas tentativas sem sucesso, para "stubar" o elemento jquery e a instância da view, no ruby com Rspec isto seria bem fácil, provavelmente eu que não sei fazer em JS com sinon.js.

Como isto ficou bem trabalhoso, acho que por hora vou decidir apenas por realizar os testes de integração, sem testes de roteador. 😔

Obrigado pela ajuda :)

/*global define, describe, it, expect, sinon */

define([
    'routes/group_user',
    'views/group_users'
],function(Router, GroupUsersView) {
    'use strict';

    describe('GroupUserRouter', function() {

        var subject;

        beforeEach(function() {
            subject = new Router();
        });

        describe('#routes', function(){
            it('routes groups to index', function() {
                expect(subject.routes.groups).to.be.eq('index');
            });
        });

        describe('#index', function() {
            beforeEach(function() {
                this.initializeSpy = sinon.spy(GroupUsersView.prototype, 'initialize');
            });

            it('renders the resources view', function() {
                subject.index();

                expect(this.initializeSpy.calledOnce).to.be.true;
            });
        });

    });

});

@renancarvalho
Copy link

Entendi, tbm da pra fazer um spy na function html do jQuery, pra isso basta carrega-lo no topo do arquivo, e fazer um spy parecido com (jquery, 'html').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment