Jasmine Model Testing

This is the first time I am writing a javascript test for the Backbone model.
Looking at a web resource, there are not many subjects on this subject.
I found this one, Testing basic applications with Jasmine and Sinon , which is pretty old (mars 2001).

In any case, I would like to know:
1) there are other resources more updates on this
2) the text I wrote (1) is good or can be improved.


(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
source share
2 answers

Perhaps SO is not suitable for viewing code, see https://codereview.stackexchange.com/ . Some notes anyway:

 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')); }); }); 

You should check for this.collection.url + '/' + 1 , because this is what you want to send to the server. Perhaps you will change the get function of your model recently, the test will pass, but the result is not what you expect.

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

This test is pointless because you are simply testing the functionality of Backbone, which we hopefully tested by the people of Backbone. The same goes for the other 2 tests. As long as you are not magical in your model, it makes no sense to test the default functionality of Backbone.

+2
source

No one tests jasmine better than Justin Searls :

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

is one example, but also check out his other presentations and his github registry for jasmine-given

+1
source

All Articles