0

我正在通过 jasmine 使用骨干和骨干.marionette 为 Web 应用程序编写测试。

我的问题是:
1)我应该在模型发生变化时检查视图以查看视图是否受到影响?
2)如果是,那么正确的方法是什么……。例如在以下情况(1)

PS:
我想避免更改模板文件


(1)

// template 

<ul class="widget-points-status">
    <li>
        <strong>{{ remaining_points }}</strong>
    </li>
    <li>
        <strong>{{ given_points }}</strong>
    </li>
    <li>
        <strong>{{ received_points }}</strong>
    </li>
</ul>

// My jasmine test could be:

describe('Changing the model:', function () {
    beforeEach(function () {
        app.currentUser.set({
            given_points: 111980,
            received_points: 892378,
            remaining_points: 435412
        });
    });

    it('it should change the points', function () {
        var pointsView = this.view.$el.text().match(/\d{1,}/)[0];
        expect(pointsView).toMatch(app.currentUser.get('given_points'));
        expect(pointsView).toMatch(app.currentUser.get('received_points'));
        expect(pointsView).toMatch(app.currentUser.get('remaining_points'));
    });

});

var pointsView = Backbone.View.extend({

    className: 'widget widget-profile',

    initialize: function () {
        app.currentUser.on('change', this.render, this);
    },

    render: function () {
        this.$el.html(profileTemplate(app.currentUser.toJSON()));

        return this;
    }
});
4

1 回答 1

1

如果您的视图侦听模型事件,您可以使用 spy 来检查您的侦听器是否在您更改模型时被调用。

describe("A Backbine View", function() {

    beforeEach(function() {
        spyOn(pointsView, "changePoints");
    });

    it("should listen to model changes", function() {
        app.currentUser.set("points", 2);
        expect(pointsView.changePoints).toHaveBeenCalled();
    });
});

当然,如果您render作为听众,同样的概念也适用。

Personally I wouldn't check the rendering explicitly in Jasmine since this will only ever work if you run the tests in a browser. If you use Jasmin for Maven for example you have Rhino and therefore no DOM to work with.

于 2012-09-13T10:06:38.723 回答