1

Backbone.View.render() と .el の操作がわかりません。render() では、表示されると予想される出力をサポートするプロパティを this.el に割り当てます。これらのプロパティは、テスト時に this.el にあり、.el はコンソールで予想される出力を返します。しかし、出力はテストに表示されません。

これがコードとテストです(粗雑さを見落とすようにしてください。私はテストをグリーンに保つために学習し、苦労しています):

var RowLabelData = Backbone.Model.extend({});

var RowLabel = Backbone.View.extend({
    initialize: function() {
           this.for_attr = this.model.get("for_attr");
           this.text_val = this.model.get("text_val");
           this.el.setAttribute("for", this.for_attr);
        },

    render: function() {
        $(this.el).html(this.text_val);
        return this;
        }
});

QUnitで次のようにテストします:

test_row_data   =   new RowLabelData({
                        for_attr: "id_username",
                        text_val: "Username:"
                    });
test_row_v      =   new RowLabel({
                        model: test_row_data,
                        tagName: 'label'
                    });
test_row_v.render();
test_row = test_row_v.el;

equal(test_row.textContent, "Username:");
equal(test_row.getAttribute("for"), "id_username");
// FAILS:
equal(test_row, '<label for="id_username">Username:</label>');

QUnit は、最後のテストで << test_row >> を返すと言います<label></label>。しかし、JavaScript コンソールでは、 << test_row >> はテキストで期待される文字列を返します。

バックボーンのドキュメントでは、render() は目的の HTML を el に配置する必要があると書かれていますが、render() のデフォルトの動作を使用しようとしており、コンソールで動作します。テストで機能しないのはなぜですか?

4

1 に答える 1

0

dira は正しいです。問題はオブジェクトと文字列の比較です。

このテスト コードは、同様の要素を作成し、それとテスト オブジェクトを文字列に変換して比較します。

new_label = document.createElement("label");
new_label.setAttribute("for", "id_username");
t = document.createTextNode("Username:");
new_label.appendChild(t);

equal(
    $('<div>').append($(test_row).clone()).remove().html(),
    $('<div>').append($(new_label).clone()).remove().html(),
    "convoluted!"
);

これはパスします。それらのクトゥルフに値する呪文のコンソール出力は"<label for="id_username">Username:</label>".

人が知ってはならない事柄にアプローチしますが、勇敢な人、または無謀な人は、ここでこの難解な秘密を発見できます。

于 2011-11-03T18:53:54.847 に答える