繁体   English   中英

使用Jasmine测试骨干模型

[英]Testing Backbone Model using Jasmine

这是我第一次为Backbone模型编写javascript测试。
查看Web资源,关于此主题的项目并不多。
我找到了这个, 用Jasmine和Sinon测试Backbone应用程序 ,这是很老的(火星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);
        });
    });

});

也许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功能是没有意义的。

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

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

是一个例子,但也看看他的其他演示文稿和他的github回购茉莉花给

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM