4

バックボーン モデルの JavaScript テストを作成するのは初めてです。
Web リソースを見ると、このテーマに関する項目はそれほど多くありません。
私はこれを見つけました、 Jasmine と Sinon を使用したバックボーン アプリケーションのテストは、かなり古いものです (2001 年 3 月)。

とにかく私は知りたいです:
1) この主題に関する他のリソースの更新があり
ます 2) 私が書いたテキスト (1) は問題ないか、改善することができます.


(1)

describe('User model', function () {
    beforeEach(function () {
        this.model = new User();
        this.collection = new UserCollection();
    });

    describe('When creating models', function () {
        it('the url should be equal at corresponding api_get_users value', function () {
            expect(this.model.url()).toEqual(backendRouter.generate('api_get_users'));
        });

        describe('when no id is set', function () {
            it('should return the collection URL', function () {
                expect(this.model.url()).toEqual(this.collection.url);
            });

        });
        describe('when id is set', function () {
            it('should return the collection URL and id', function () {
                this.model.set({id: 1});
                expect(this.model.url()).toEqual(this.collection.url + '/' + this.model.get('id'));
            });
        });

    });

    describe('when fetching model from server', function () {
        beforeEach(function () {
            this.model.set({id: 1});
            this.fixture = this.fixtures.Users.valid;
            this.fixtureResponse = this.fixture.response.users[0];
            this.server = sinon.fakeServer.create();
            this.server.respondWith(
                'GET',
                backendRouter.generate('api_get_users') + '/' + this.model.get('id'),
                JSON.stringify(this.fixtureResponse)
            );
        });

        afterEach(function () {
            this.server.restore();
        });

        it('should make the correct request', function () {
            this.model.fetch();
            expect(this.server.requests.length).toEqual(1);
            expect(this.server.requests[0].method).toEqual('GET');
            expect(this.server.requests[0].url).toEqual(this.model.url());
        });

        it('should the response not change', function () {
            this.model.fetch();
            this.server.respond();
            expect(this.fixtureResponse).toEqual(this.model.attributes);
        });

        it('should exhibit mandatory attributes', function () {
            expect(this.model.get('id')).toBeGreaterThan(0);
        });
    });

});
4

2 に答える 2

2

たぶん、SOはコードレビューに適した場所ではありません。https://codereview.stackexchange.com/をご覧ください。とにかくいくつかのメモ:

describe('when id is set', function () {
    it('should return the collection URL and id', function () {
        this.model.set({id: 1});
        expect(this.model.url()).toEqual(this.collection.url + '/' + this.model.get('id'));
    });
});

テストする必要this.collection.url + '/' + 1があります。これがサーバーに送信したいものだからです。get後でモデルの機能を変更するかもしれません。テストは合格しますが、結果は期待したものではありません。

it('should make the correct request', function () {
        this.model.fetch();
        expect(this.server.requests.length).toEqual(1);
        expect(this.server.requests[0].method).toEqual('GET');
        expect(this.server.requests[0].url).toEqual(this.model.url());
    });  

このテストは無意味です。バックボーンの機能をテストするだけで、バックボーンの人々によってテストされることを願っています。他の2つのテストについても同じです。モデルに魔法をかけない限り、デフォルトのバックボーン機能をテストしても意味がありません。

于 2012-07-18T19:22:00.853 に答える
1

Justin Searlsほどジャスミンテストをカバーしている人はいない:

https://speakerdeck.com/u/searls/p/confidencejs

は一例ですが、彼の他のプレゼンテーションとジャスミンのgithubリポジトリもチェックしてください-与えられた

于 2012-07-18T13:27:16.663 に答える