Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:43:08+00:00 2026-06-14T02:43:08+00:00

I have a controller which relies on a service built through ngResource. I am

  • 0

I have a controller which relies on a service built through ngResource. I am having trouble testing this(although both appear to work like a charm in the actual application). The following is the (sanitized) Controller

MyApp.Controller.MyCarsController = (scope, http, typeService) ->
  if scope.context==undefined
    scope.ferrari_or_porshe =""
    scope.id = ""
  else if scope.context=="ferrari"
    scope.country_or_pi ="Ferrari"
  else if scope.context=="porshe"
    scope.country_or_pi ="Porshe"

  typeService.index
    ferrari_or_porshe: scope.ferrari_or_porshe
    id: scope.id
  , (response) ->
    scope.type = response
    scope.loading = false

MyApp.Controller.MyCarsController.$inject = ['$scope', '$http', 'Type']

And this is the Service:

MyApp.MyModule.factory 'Type', ['$resource', ($resource) ->
  TypeResource = $resource("/api/types/:ferrari_or_porshe/:id", {},
    index:
      method: "GET"
      isArray: true
  )
  return TypeResource
]

Finally, some test code:

describe 'MyApp.Controller.MyCarsController', ->
  beforeEach module('MyModule')
  beforeEach inject ($rootScope, $http, $controller, Type) ->
    @scope = $rootScope.$new()
    @typeService = Type
    @scope.context = undefined
    $controller 'MyApp.Controller.MyCarsController', $scope: @scope

  describe '#home-page', ->

    it 'contains a list of types', ->
      expect(@scope.types.length).toBeGreaterThan 0

    it "sets instance variables correctly", ->
      expect(@scope.ferrari_or_porshe).toBe ""
      expect(@scope.id).toBe ""

Which fails with:

  No more request expected in helpers/angular-mocks.js on line 889
  TypeError: 'undefined' is not an object (evaluating 'this.scope.types.length') in controllers/my_cars_controller_spec.js

By judicious application of console.logs, I have discovered that the issue is that the final callback on response is never reached. TypeResource comes back as [Function].

My questions is:

How do I drive the Jasmine Tests to correctly enter the Service and fetch a response? And is there any way to create direct Unit Tests for Services?

Any and all help is appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:43:10+00:00Added an answer on June 14, 2026 at 2:43 am

    The Solution is as follows: for the Service, use $httpBackend which is bundled as part of ngMock:

    http://code.google.com/p/google-drive-sdk-samples/source/browse/ruby/app/lib/angular-1.0.0/angular-mocks-1.0.0.js?r=c43c943e32be395b7abca8150deb301d3cbc0dbe

    Use this to mock the Rest responses. Since in my case I only cared about verifying that a GET request goes out:

     describe 'Type', ->
       describe '#index', ->
         beforeEach module('MyModule')
         beforeEach inject(($httpBackend) ->
           $httpBackend.whenGET('/api/types/ferrari/1').respond([])
         )
    
         it 'for a ferrari scope', inject((Type) ->
           ferrari_or_porsche = 'ferrari'
           id = '1'
           expect( Type.index('ferrari_or_porsche': ferrari_or_porsche, 'id': id) ).toEqual([ ])
         )
    

    And then for the controller, mock the service using jasmine spies and use jasmine.any(Function) to warn of the callback.

     describe 'MyApp.Controller.MyCarsController', ->
       beforeEach module('myModule')
       beforeEach inject ($rootScope, $http, $controller, Type) ->
         @scope = $rootScope.$new()
         @typeService = Type
         @scope.context = undefined
         spyOn(@typeService, 'index')
    
       describe '#home-page', ->
         beforeEach inject ($controller) ->
           $controller 'MyApp.Controller.MyCarsController', $scope: @scope
         it 'contains a list of types', ->
           expect(@typeService.index).toHaveBeenCalledWith({ ferrari_or_porsche : '', id : '' }, jasmine.any(Function))
    
         it "sets instance variables correctly", ->
           expect(@scope.ferrari_or_porsche).toBe ""
           expect(@scope.id).toBe ""
    

    Note: I make no claims as to the “canonicalness” of this solution. But it works.
    Note: The API endpoints are of course tested extensively elsewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this controller which performs me a multiple domain databinding class, and it
I have this controller which will simply change the language in a session so
I have a controller which renders a view (~/Views/Component/Create.aspx) no problem. When this view
I have a controller which loads the view like this: class A extends Controller{
Hi guys i have controller which returns a partial view, the controller is called
I have a Controller which returns a ViewModel to a View and it works
I have a controller which has a method called history class UsersController < ApplicationController
i have view controller which contains a button which show the image library ,if
I have an controller which is an ArrayProxy and contains some computed arrays. In
I have a controller function which return a response with a value, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.