Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to mock angular $resource in jasmine tests
    primarykey
    data
    text
    <p>I am trying to test a controller which uses angular's $resource.</p> <pre><code>function PermissionsCtrl($scope, $resource, $cookies) { var Object = $resource('/v1/objects/:id'); loadObjects(); function loadObjects() { $scope.myAppObjects = new Array(); var data = AppObject.get({find: '{"app_id": '+wtm.app_id+'}'}, function(){ if (data.results) { for(var i = 0; i&lt; data.results.length; i++) { if(!data.results[i].is_deleted) { (function(i){ $scope.objects(data.results[i]); }(i)); } } } }, function(error){console.log(error);}); } </code></pre> <p>And here is the test code.</p> <pre><code>var apiServer = "..."; var app_id = 999 var mock_object_data = {...}; describe('apps permissionsCtrl', function(){ var scope, ctrl, $httpBackend; // Create a matcher for comparing data beforeEach( function() { this.addMatchers({ toEqualData: function(expected) { return angular.equals(this.actual, expected); } }); }); // Create the controller with injected data/services beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, $resource) { $httpBackend = _$httpBackend_; // cookie data to inject var cookies = new Array(); cookies['id'] = '...'; // just needs to be declared $httpBackend.expectGET(apiServer+'/v1/app_objects? find=%7B%22app_id%22:'+app_id+'+%7D&amp;access_token=' + cookies['id']) .respond( mock_object_data ); var $injector = angular.injector(['ng', 'ngResource']); var $resource = $injector.get('$resource'); scope = $rootScope.$new(); ctrl = $controller(PermissionsCtrl, {$scope: scope, $cookies: cookies, $resource: $resource}); })); it('should put object data into $scope', function() { $httpBackend.flush(); expect(scope.objects).toEqualData( mock_object_data ); </code></pre> <p>}); });</p> <p>When I run this I get </p> <pre><code>Error: Unknown provider: $resourceProvider &lt;- $resource </code></pre> <p>at the line where I try to create my controller. I don't understand how to inject this into my controller and no matter what I try I get the same error. A couple things I've tried are </p> <ul> <li>Declaring a an empty mock object and passing it through similar to my cookies variable. I figure this is probably a bad solution anyway since I actually want to use the service. </li> <li>Mimicking the scope mock and passing it into my inject function and passing $resource.$new() to my controller. </li> <li>Doing nothing and hoping that httpBackend would cover it since that's what ultimately gets called anyway. Vojta Jína made it sound like that would work but no dice.</li> <li>Mild epithets. Satisfying but not very effective.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload