Home  >  Q&A  >  body text

javascript - backbone怎么做单元测试?

项目主要用require&backbone,想要好好重构一下,想一边重构一边写测试。

怪我咯怪我咯2653 days ago258

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-10 15:08:38

    做过两个Backbone RequireJS jQuery Mustache结合的项目。

    一个用的是Selenium,可以自动Google。
    一个是自己在Github上的项目: https://github.com/phodal/freerice

    说说第二个,测试有两种

    单元测试

    用的是: Sinon + Jasmine + Jasmine-jQuery。

    测试Model的逻辑,需要用到FakeServer,如: https://github.com/phodal/freerice/blob/master/web/test/spec/RiceModel...

    beforeEach(function() {
        this.server = sinon.fakeServer.create();
        this.rices = new Rices();
        this.server.respondWith(
            "GET",
            "http://0.0.0.0:8080/all/rice",
            [
                200,
                {"Content-Type": "application/json"},
                JSON.stringify(data)
            ]
        );
    });
    

    测试View的话,基本就靠行为,如freerice/web/test/spec/LoginViewSpec.js

    beforeEach(function() {
        view = new LoginView();
        view.$el.find('#username').val('admin').trigger('change');
        view.$el.find('#password').val('admin').trigger('change');
    });
    

    功能测试

    因为用的是Node,所以用的是zombie

    一个简单的测试用例如下freerice/test/system/function_test.js:

        it('should be on login page', function(done) {
            browser.visit('#/account/login')
                .then(function() {
                    assert.equal(browser.location.href, website + '#/account/login', 'It is not the Login page');
                    assert.ok(browser.success, 'It did not load successfully.');
                })
                .then(done, done);
        });
    

    reply
    0
  • Cancelreply