Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@byrichardpowell
Created October 9, 2012 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byrichardpowell/3857905 to your computer and use it in GitHub Desktop.
Save byrichardpowell/3857905 to your computer and use it in GitHub Desktop.
Model Fetch Unit Test
describe("A Model should", function() {
// FETCH
describe( "have a fetch method that", function() {
beforeEach(function() {
urls = ['url1', 'url2', 'url3', 'url4', 'url5']
spyOn( $, 'ajax');
});
it( "makes an AJAX request for each URL it is passed", function() {
model.fetch( urls );
expect( $.ajax ).toHaveBeenCalled();
expect( $.ajax.callCount ).toEqual( urls.length )
})
})
})
@pootsbook
Copy link

It looks good. Here’s my take:

describe("Model", function() {
  describe("fetch", function() {
    beforeEach(function() {
      urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
      spyOn($, 'ajax');
    });
    describe("with parameter Array", function () {
      it("calls $.ajax for each URL in the Array", function() {
        model.fetch(urls);
        expect($.ajax.callCount).toEqual(urls.length)
      });
    });
  });
});

Comments

Opinion:

  • Descriptions should be short and tied to the class/method names
  • Let the descriptions make comments redundant Uncle Bob
  • Use semi-colons consistently
  • Make one expectation per spec

Fact:

  • Using callCount makes the expectation that $.ajax was called redundant

Interested to hear others’ thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment