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

  • Home
  • SEARCH
  • 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 9205481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:57:35+00:00 2026-06-17T23:57:35+00:00

We have a what we call a CORShttpService , which is basically a wrapper

  • 0

We have a what we call a CORShttpService, which is basically a wrapper aroung the $http service, but encapsulates some CORS functionality that we need. I’m now writing some tests for a service that has the CORShttpService injected into it. This service has code like this:

CORShttpService({method: requestMethod, url: getUrl(path), data: data}).
    success(function(data, status, headers) {
        //do success stuff
    }).
    error(function(data, status, headers) {
       //do error stuff
    });

I want to mock the call to CORShttpService, but I’m not sure how to go about doing it. I’m using Jasmine, and its spyOn function requires an object to mock the function on the object. My CORShttpService isn’t attached to any object, so I don’t know how to go about mocking it. Yes, I could use $httpBackend to mock the requests that eventually get set in the CORShttpService, but I don’t want it going into that service in the first place. I want to isolate the unit test and simply mock the external calls. Is there any way I can mock this service that is just a function?

  • 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-17T23:57:36+00:00Added an answer on June 17, 2026 at 11:57 pm

    As I thought about this more, the $httpBackend service does provide a lot of functionality for testing requests. As my CORShttpService is a basically a wrapper around $http, I decided I could probably get the most bang for my buck, if I made the mock implementation of CORShttpService simple the $http implementation. Using this documentation to help me, I have the following in my spec:

    beforeEach(module(function($provide) {
        $provide.provider('CORShttpService', function() {
            this.$get = function($http) {
                return $http;
            };
        });
    }));
    

    So, any of my services wanting the CORShttpService injected, will now basically just have $http injected, and thus allow me to use all the $httpBackend functionality without the concern of the extra functionality found in the CORShttpService itself.

    This works for my specific case, but as far as a general solution for mocking services that are just a function, the same kind of thing could probably be done with jasmine.createSpy as mentioned in zbynour’s answer. Something like:

    beforeEach(module(function($provide) {
        $provide.provider('MyService', function() {
            this.$get = function() {
                return jasmine.createSpy("myService");
            };
        });
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to call a remote service which expects me to send some parameters
I have a call to powershell.exe that looks like this, and which works from
I have to call domain A.com (which sets the cookies with http) from domain
I have to call a Web service that is extremely demanding (unstandard) regarding the
I have to call some badly written 3rd party COM components that have memory
I am currently making a Cordova plugin that will have call a method which
I have this call that makes a client log in, but how do i
I have to call some code in a SharePoint site that runs under the
I have a call to a third-party C++ library which I have put into
I have a call to Read on NetworkStream objeck, which uses Socket.Receive internally. Say

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.