Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 4: test MultiRecipeLoader spec suggestions & fix #17

Open
so0k opened this issue Feb 11, 2014 · 0 comments
Open

Chapter 4: test MultiRecipeLoader spec suggestions & fix #17

so0k opened this issue Feb 11, 2014 · 0 comments

Comments

@so0k
Copy link

so0k commented Feb 11, 2014

current github code:

  describe('MultiRecipeLoader', function() {
    var mockBackend, recipe, loader;
    // The _$httpBackend_ is the same as $httpBackend. Only written this way to
    // differentiate between injected variables and local variables
    beforeEach(inject(function(_$httpBackend_, Recipe, MultiRecipeLoader) {
      recipe = Recipe;
      mockBackend = _$httpBackend_;
      loader = MultiRecipeLoader;
    }));

    it('should load list of recipes', function() {
      mockBackend.expectGET('/recipes').respond([{id: 1}, {id: 2}]);

      var recipes;

      var promise = loader();
      promise.then(function(rec) {
        recipes = rec;
      });

      expect(recipes).toBeUndefined();

      mockBackend.flush();

      expect(recipes).toEqualData([{id: 1}, {id: 2}]);
    });
  });
  1. first I'd suggest the usage of leading and trailing underscores for all injected variables. the book is not explaining these are ignored by the injector and using this notation helps to highlight injected variables.
  2. second, I'd like to point out that the spec will fail as "loader" is the actually promise object and not a function.

my suggestion would be: (this is wrapped in a services test suite where the Recipe resource and $httpBackend mock are scoped for all individual service test suites)

var Recipe,
    $httpBackend;

  describe('MultiRecipeLoader', function(){
    var MultiRecipeLoader;
    //injector will ignore front and trailing underscore, this notation is to clarify injected variables
    //yet give the ability to write tests using variables exactly the same way the actual implementations do
    beforeEach(inject(function (_$httpBackend_, _Recipe_, _MultiRecipeLoader_) {
      Recipe = _Recipe_;
      $httpBackend = _$httpBackend_;
      MultiRecipeLoader = _MultiRecipeLoader_;
    }));

    it('should load a list of recipes', function () {
      $httpBackend.expectGET('/recipes').respond([{id:1}, {id:2}]);

      var recipes;
      var promise = MultiRecipeLoader;
      promise.then(function(rec){
        recipes = rec;
      });

      expect(recipes).toBeUndefined();

      $httpBackend.flush();

      expect(recipes).toEqualData([{id:1},{id:2}]);
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant