Fixed error returning the object ($$ state)

I watched Angular Play by Play testing on PluralSight by John Pap and Ward Bell.

I am currently getting the following error when starting my specs.

AssertionError: expected { Object ($$state) } to have a property 'length' at Assertion.assertLength (bower_components/chai/chai.js:1331:37) at Assertion.assert (bower_components/chai/chai.js:4121:49) at Context.<anonymous> (scripts/home/homeController.Specs.js:48:49) 

Please note that I only included the code, which, in my opinion, matters, so I do not overload this question with irrelevant information. If you need to see more code, this is not a problem.

My code is as follows:

homeController.js:

 window.app.controller('homeController', ['$scope', 'sidebarService', function ($scope, sidebarService) { $scope.title = 'Slapdash'; $scope.sidebar = { "items": sidebarService.getSidebarItems() }; }]) 

sidebarService.js:

 (function () { window.app .service('sidebarService',['$http', function ($http) { this.getSidebarItems = function () { $http.get("http://wwww.something.com/getSidebarItems") .then(function (response) { return response.data; }); }; }]); }()); 

homeController.Specs.js:

beforeEach:

 beforeEach(function () { bard.appModule('slapdash'); bard.inject(this, '$controller', '$q', '$rootScope') var mockSidebarService = { getSidebarItems : function(){ return $q.when(mockSidebarMenuItems); } }; controller = $controller('homeController', { $scope: scope, sidebarService: mockSidebarService }); }); 

specification error:

  it('Should have items', function () { $rootScope.$apply(); expect(scope.sidebar.items).to.have.length(mockSidebarMenuItems.length); // same number as mocked expect(sidebarService.getSidebarItems).to.have.been.calledOnce; // it a spy }); 
+8
javascript angularjs bardjs
source share
1 answer

The answer was that I was returning the result from the service, not a promise.

  $http.get("http://wwww.something.com/getSidebarItems") .then(function (response) { return response.data; // <- returning data not promise }); 

When I mocked, I used

 var mockSidebarService = { getSidebarItems : function(){ return $q.when(mockSidebarMenuItems); } }; 

who mocks the promise. However, I just needed to return the data, as the promise was expected in the service.

  mockSidebarService = { getMenuItems : function(){ return mockSidebarMenuItems } }; 

I made a change and it all works now. Took the time, but at least that made sense.

+3
source share

All Articles