4

so I'm new to angularjs and its mocking library. I am trying to test that a specific GET request is made, but I always get this error for the 2nd assertion and can't figure out why:

Error: Unsatisfied requests: GET /1.json

Is there anything I messed up with my code below?

App.js

var App = angular.module('App', []).config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/', {
    templateUrl: 'views/main.html',
    controller: 'MainCtrl'
  }).when('/Items', {
    templateUrl: 'views/items.html',
    controller: 'Ctrl'
  }).otherwise({
    redirectTo: '/'
  });
}]);

Ctrl.js

function Ctrl($scope, $http, $filter) {
  $scope.items = [];

  $http.get('/1.json').success(function(data) {$scope.items = data.items;});
}

Ctrl.$inject = ["$scope","$http", "$filter"];

Spec/Ctrl.js

describe('Controller: Ctrl', function() {
  var $httpBackend;
  // load the controller's module
  beforeEach(module('App'));
  beforeEach(inject(function($injector) {
    $httpBackend = $injector.get('$httpBackend');

    // backend definition common for all tests
    $httpBackend.whenGET('/1.json').respond('Response!');
  }));

  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  var Ctrl, scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function($rootScope, $controller) {

    scope = $rootScope.$new();
    Ctrl = $controller('Ctrl', {
      $scope: scope
    });
  }));

  it('should initialize with 0 items', function() {
    expect(scope.items.length).toBe(0);
    $httpBackend.flush();
  });

  it('should make store request', function(){
    var controller = scope.$new(Ctrl);
    $httpBackend.expectGET('/1.json');
    $httpBackend.flush();
  });
});

EDIT: added app and controller code.

2
  • Your code indicates that you expect a "get" request, but I don't see where it's made, which is why you're getting the error. Where are you expecting that request to happen? Commented Feb 8, 2013 at 8:41
  • Sorry I forgot to include the app and controller code. I've done that, is my error happening perhaps due to the routeProvider? do I need to tell my test to execute a route somehow? Commented Feb 8, 2013 at 12:15

1 Answer 1

5

I finally got my unit tests working! Mostly because I restructured my application to make more sense and be more modular.

I'll try to give information to help the next person that runs into this:

first of was I switched to using the $resource instead of $http.

instead of injecting $injector, I injected $httpBackend like so:

beforeEach(inject(function(_$httpBackend_, $rootScope, $route,  $controller){

  $httpBackend = _$httpBackend_;
  $httpBackend.expectGET('/path/to/api').respond([{id:1}]);

instead of referencing 'Ctrl' as a string, I passed in the actual class

Ctrl = $controller('Ctrl', { $scope: scope });

became

var ProductsCtrl = ['$scope', function($scope){ ... }];

Ctrl = $controller(ProductsCtrl, {
  $scope: scope
});`

Make sure you are referencing the angular-resources.js file if you are using $resources

I'm really loving Angularjs; I think it just takes some time to wrap your head around how to test. Best of luck out there!

Sign up to request clarification or add additional context in comments.

1 Comment

can u take a look at this? i m newbie, and it looks similar to urs. stackoverflow.com/questions/15981583/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.