Skip to content

Instantly share code, notes, and snippets.

@zdwolfe
Created September 28, 2013 16:18
Show Gist options
  • Save zdwolfe/6743666 to your computer and use it in GitHub Desktop.
Save zdwolfe/6743666 to your computer and use it in GitHub Desktop.
user.js:
'use strict';
angular.module('myApp')
.controller('UserCtrl', function ($scope, Restangular) {
$scope.users = Restangular.all('user').getList();
});
user_test.js (test):
'use strict';
describe('Controller: UserCtrl', function () {
beforeEach(module('myApp'));
var UserCtrl;
var scope;
var httpBackend;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
httpBackend = $httpBackend;
var fauxUsersList = [ 'foo' ];
// Create a faux HTTP response on HTTP-GET /api/user
httpBackend.when('GET', '/api/user').respond(fauxUsersList);
scope = $rootScope.$new();
$controller('UserCtrl', { $scope: scope });
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should attach a faux users list to scope', function () {
httpBackend.flush();
console.log(JSON.stringify(scope.users));
expect(scope.users.length).toBe(1);
});
});
@Dashue
Copy link

Dashue commented Sep 29, 2013

'use strict';
angular.module('myApp', ["restangular"])

.controller('UserCtrl', function ($scope, Restangular) {
    Restangular.all('user').getList().then(function(users) {
        $scope.users = users;
    });
});

describe('Controller: UserCtrl', function () {
beforeEach(module('myApp'));

var UserCtrl;
var scope;
var httpBackend;

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

    var fauxUsersList = [ 'foo' ];

    // Create a faux HTTP response on HTTP-GET /api/user
    httpBackend.when('GET', '/user').respond(fauxUsersList);

    scope = $rootScope.$new();

    $controller('UserCtrl', {
         $scope: scope
    });
}));

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

it('should attach a faux users list to scope', function () {
    httpBackend.flush();

    console.log(JSON.stringif   y(scope.users));
    expect(scope.users.length).toBe(1);
});

});

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