【问题标题】:Testing Backbone Model using Jasmine使用 Jasmine 测试主干模型
【发布时间】:2012-07-18 13:02:56
【问题描述】:

这是我第一次为 Backbone 模型编写 JavaScript 测试。
在网络资源中查找有关此主题的项目并不多。
我找到了这个,Testing Backbone applications with Jasmine and Sinon,它已经很老了(2001 年火星)。

无论如何我想知道:
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);
        });
    });

});

【问题讨论】:

标签: javascript backbone.js jasmine


【解决方案1】:

也许 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());
    });  

此测试毫无意义,因为您只是测试 Backbone 功能,希望由 Backbone 人员进行测试。其他 2 个测试相同。只要您不在模型中使用魔法,测试默认的 Backbone 功能是没有意义的。

【讨论】:

    【解决方案2】:

    没有人比 Justin Searls 更好地涵盖茉莉花测试:

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

    是一个例子,但也请查看他的其他演示文稿和他的jasmine-given github repo

    【讨论】:

    • +1 感谢您的参考。实际上,在我的问题中,我问的是更具体的问题:测试骨干模型。在 jasmine-given 存储库中,我找不到我要找的东西。
    猜你喜欢
    • 2012-08-10
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多