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 8431963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:59:42+00:00 2026-06-10T05:59:42+00:00

I have a function on my service that looks like something this: addStatement: function(user,

  • 0

I have a function on my service that looks like something this:

addStatement: function(user, action, object) {
            var statement = {
                    user: user,
                    action: action,
                    object: object
            };
            $http({
                method: 'POST', 
                url: '/foo/bar', 
                data: statement, 
                headers: {Authorization: 'Basic YWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
            }).success(function(data) {
                alert("success: " + data);
            });
        }

I want to write a unit test for this method, but I’m not sure how to make it work. Basically, I want to test that the data sent up was correctly constructed from the function parameters and that the correct Authorization header was sent up.

I’ve been reading over how to use $httpBackend, but the example there is testing a controller, and just stuff that changes on the $scope when the requests are made and returned. Is there a way to achieve the tests I want? Or am I going about something wrong?

  • 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-10T05:59:44+00:00Added an answer on June 10, 2026 at 5:59 am

    Testing services doesn’t differ much from testing controllers so the principles are the same. Basically you need to:

    • Inject objects under test
    • setup mocks (if any) – here you were on the right track using $httpBackend mock
    • run methods on your object under test and verify the results

    If you are after testing that a service methods results in $http POST call you could write your test like this (non-essential parts omitted):

    beforeEach(module('MyApp'));
    beforeEach(inject(function(MyService, _$httpBackend_) {
        service = MyService;
        $httpBackend = _$httpBackend_; // angular strips the underscores so
                                       // we don't need to use unique names
        // https://docs.angularjs.org/api/ngMock/function/angular.mock.inject
    }));
    
    it('should invoke service with right paramaeters', function() {
        $httpBackend.expectPOST('/foo/bar', {
            "user": "testUser",
            "action": "testAction",
            "object": {}
        }, function(headers){
            return headers.Authorization === 'Basic YWxhZGRpbjpvcGVuIHNlc2FtZQ==';
        }).respond({});
        service.addStatement('testUser', 'testAction', {});
        $httpBackend.flush();
    });
    

    Here is the working jsFiddle illustrating this test in action: http://jsfiddle.net/pkozlowski_opensource/MkrjZ/2/

    One last remark: it is better not to use .alert() in unit tests since those alerts will pause execution of tests.

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

Sidebar

Related Questions

I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I have a WebMethod that recives HTML as parameter somthing like this: Public Function
I have a WCF service with a function GetData() When my client calls this
I have a REST-like PHP service and i don't understand how invoke function of
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =
I have successfully set up a quick test of creating a REST-like service that
I have a service that runs this thread to get GPS coordinates LocationManager lm
I have something like this on my view: <input type=checkbox value=1 name=services-for /> <input
Normally the code for using the dbContext from Entity Framework looks something like this:
I made a Web service in which I have a function to count some

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.