Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Unit Testing Chapter with Jasmine Examples #310

Closed
AndrewHenderson opened this issue Feb 6, 2013 · 5 comments
Closed

Unit Testing Chapter with Jasmine Examples #310

AndrewHenderson opened this issue Feb 6, 2013 · 5 comments

Comments

@AndrewHenderson
Copy link

Wasn't sure where to start a discussion thread on this, but I disagree with instructing developers to test whether a model returns the values passed to it. In my opinion, the code example below, taken from the current version of the book, is actually a test of the Backbone library's get() method.

it('Will set passed attributes on the model instance when created.', function() {
    var todo = new Todo({ text: 'Get oil change for car.' });

    // what are the values expected here for each of the
    // attributes in our Todo?

    expect(todo.get('text')).toBe('Get oil change for car.'');
    expect(todo.get('done')).toBe(false);
    expect(todo.get('order')).toBe(0);
});

Even if you are testing that the engineer wrote the constructor Todo() correctly, should we be testing every model for such basic behavior? If so, where do you draw the line?

You could make the same argument for testing every core functionality of a Backbone model. Why not check save() or clone() for that matter; destroy(), hasChanged(). The list goes on.

@addyosmani
Copy link
Owner

Would you as a reader prefer that the example be shown with a note stating that not every model should be tested and that this is simply a reference example or would you prefer for this example to be scrapped entirely.

@AndrewHenderson
Copy link
Author

I would say remove it. At best, it's a intro to Jasmine and should be in a different book, in my opinion. At worst, it teaches developers to test code that's already been tested.

I would suggest we teach readers to test the code they write. So, if you override a function - for instance, a model's fetch(), then yes, write a test for it.

Personally, I keep my models light, using Backbone's built in functionality, and so I don't usually test them as often as I do my views.

I test my view's functions. I treat each function as a unit to be tested to ensure that the block of code I wrote behaves as expected.

@sindresorhus
Copy link
Contributor

I agree with @AndrewHenderson, seems moot.

@addyosmani
Copy link
Owner

I agree as well.

@krawaller
Copy link

I've found that it is easier to scope tests to my own code and not Backbone's if I don't instantiate the models/views. Instead I just test the methods directly on the prototype, often using fake contexts with spies. I did a small writeup of the method here.

While it might need a high level of comfort with unit testing, Jasmine and spies, I still feel it makes testing Backbone object so much easier that it qualifies for being mentioned in the testing section. If you find any of it useful, @addyosmani , borrow away!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants