0

ジャスミンを介してバックボーンと backbone.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

ビューがモデル イベントをリッスンする場合、スパイを使用して、モデルを変更したときにリスナーが呼び出されるかどうかを確認できます。

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リスナーとして持っている場合も同じ概念が適用されます。

個人的には、Jasmine でレンダリングを明示的にチェックすることはありません。これは、ブラウザーでテストを実行した場合にのみ機能するためです。たとえば、Maven に Jasmin を使用する場合、Rhino があるため、使用する DOM がありません。

于 2012-09-13T10:06:38.723 に答える