You'll want to inject $httpBackend

var $httpBackend;
beforeEach(inject(function ( _$httpBackend_) {
            $httpBackend = _$httpBackend_

Then inside your test invoke $httpBackend.when() to mock the getting of the 
data.

$httpBackend.when('GET',
                        '/some-url')
                    .respond(200, someData);
                Foo.then(function (data) {
                    expect(data.length).toBeGreaterThan(0);


On Friday, March 28, 2014 4:25:59 PM UTC-7, Daniel Suarez wrote:
>
> Hey All, 
>
> I'm trying to get some tests but it is not working. I have a controller 
> that calls a service 
>
> var myCtrl = function($scope, someData) {
>
>         $scope.someData = someData;
>         someData.getCurrentQuestion();
>         
>     };
>     return [ "$scope", 'someData',  myCtrl ];
>
> I have a service that gets data from an $http 
>
> var someData = function($http) {
>         var Data = {
>             getCurrentQuestion: getCurrentQuestion,
>             
>         };
>         function getCurrentQuestion() {
>             $http.get('PATH').success(function(data) {
>                 Data.questionId = data.questionId;
>                 Data.text = data.text;
>                 Data.kind = data.kind;
>             })
>
>         }
>         return Survey
>     };
>     return ['$http', someData]
>
> Now from the following test that is not working, how would i fake data for 
> the $http request. This is my non-working code. I would really appreciate 
> your help.  
>
> describe('myCtrl', function() {
>         var $scope,
>             mockService,
>             firstQuestion = {
>                 "questionId": "gender",
>                 "text": "blah",
>                 "kind": true,
>             };
>
>         beforeEach(module('myModule'));
>         beforeEach(inject(function($rootScope, $controller,_someData_, $q) 
> {
>             $scope = $rootScope.$new();
>             mockService = _someData_;
>             spyOn(mockService, 'getCurrentQuestion')
>                 .andCallFake(function() {
>                     return firstQuestion
>                 });
>             $controller('myCtrl', {$scope: $scope, someData: mockService})
>         }));
>
>         beforeEach(function(){
>             $scope.$digest();
>         });
>
>
>
>         describe('someService', function() {
>             it('ensures test passes' , function() {
>                 expect($scope.someData. questionId).toBe('gender');
>             });
>             
>             it(''ensures test passes'', function() {
>                 expect($scope.someData.kind).toBe(true);
>             });
>
>         })
>
>     })
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to