Using Angular and Jasmine: techniques for simulating a service that provides a promise

In my AngularJS application, I have a controller called MenuCtrl that utilizes a service provided by "$mdSidenav" from Angular Material. This service is created using a factory method.

angular.module('leopDirective', [])
    .controller('MenuCtrl', function ($scope,$timeout,$mdSidenav,$log) {

    $scope.mdSidenav = $mdSidenav;
    $scope.close = function () {
        $mdSidenav('left').close().then(function() {$scope.closed=true; });
    };

});

I am currently attempting to mock the $mdSidenav service in order to test my controller. Below is the Jasmine test code I am working on:

describe('Controller: MenuCtrl', function() {
    var $rootScope, $scope, $controller, $q, menuCtrl,
        mock__mdSidenav = function(component) {
            return {
                // THIS FUNCTION IS EMPTY SINCE $Q IS NOT AVAILABLE
                close: function() {}
            }
        };

    beforeEach(function() {
        module('leopDirective', function($provide) {
            // MOCK UP MUST BE PROVIDED BEFORE INJECTING $Q
            $provide.value('$mdSidenav', mock__mdSidenav);
        });
        inject(function($injector) {
            $rootScope = $injector.get('$rootScope');
            $controller = $injector.get('$controller');
            $q = $injector.get('$q');
            $scope = $rootScope.$new();
        });
        menuCtrl = $controller("MenuCtrl", { $scope: $scope });
    });

    it('should create the $mdSidenav object', function() {
        var deferred = $q.defer(),
            promise = deferred.promise;
        // NOW THAT $Q IS AVAILABLE, I TRY TO FILL UP THE FUNCTION
        mock__mdSidenav.close = function() {
            return promise.then(function(response){return response.success;});
        };
        // force `$digest` to resolve/reject deferreds
        $rootScope.$digest();
        // INVOKE FUNCTION TO TEST
        $scope.close();
    });
});

The issue I'm facing is figuring out how to create a function in the mock that returns a promise:

  1. Creating a promise relies on $q,
  2. $q needs to be injected within the "inject" block,
  3. However, $provide must be used inside the "module" block before injecting,
  4. Therefore, the function in the mock object (mock__mdSidenav().close()) has to be empty before invoking "$provide" and then somehow filled later.

Unfortunately, this approach is leading to an error message (see Demo plnkr), indicating that creating an empty function first and filling it later does not work:

TypeError: Cannot read property 'then' of undefined
    at Scope.$scope.close (app.js:7:35)

What is the correct way to mock a service with a function that returns a promise?

Answer №1

To start, ensure that your mock service method returns a Promise object:

mock__mdSidenav = function(component) {
  return {
    isMock: true, 
    close: function() {
        return $q.when();
    }
  }
};

Next, inject it into the controller instance:

menuCtrl = $controller("MenuCtrl", { 
    $scope: $scope,
    $mdSidenav: mock__mdSidenav
});

Lastly, establish some expectations in your test:

it('should create the $mdSidenav object', function() {

  $scope.close();
  $rootScope.$digest();

  expect($scope.closed).toBe(true);
});

Check out the demo here: http://plnkr.co/edit/dr79GgZYG2tjiuWU0oFx?p=preview

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The error message that keeps popping up is saying that it cannot access the property "username" as it is undefined

signup.js const SignupForm = () => { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const handleSignup = () => { fetch("/signup", { method: "post", header ...

Unable to locate element in Internet Explorer when using frame switching within Selenium

I am currently working on a project in Selenium that is specifically designed to run in Internet Explorer, but I am encountering issues locating the xpath element. Despite this setback, I managed to make progress in the test by using the switch frame func ...

Dynamic JavaScript Line Graph

I am facing a challenge with a small function in my application that needs to display real-time data on a webpage. I have been exploring different Javascript examples, such as the Flot example "real-time updates" and the Highcharts example "spline updating ...

Encountering a RuntimeError during the integration of Angular Js in Rails with ExecJS

Currently, I'm working on integrating Angular Js into a Rails Project and I've been following the tutorial provided at . However, after completing all the steps, I encountered the following error: https://i.sstatic.net/ehYCb.png I've searc ...

Looking to add a close button to the iFrame, but having trouble getting it to function when clicked

In my project built with Meteor, I have integrated an iframe to display specific content to the user. The iframe is functioning as expected, but I am looking to add a close button that will effectively unload the iframe when clicked. I attempted to imple ...

Switch out regex with an equal number of characters

I am looking for a way to replace specific content using a regex with the same number of characters but replacing them with a character of my choice. For instance: content: "abcdefghi*!?\"asd"; should be transformed into: content: "--------------- ...

Take the attention away from the canvas

Is there a way to shift focus away from the canvas and onto input fields in my HTML code? Right now, the canvas is holding onto focus even when I'm clicking on text inputs. This prevents me from typing anything into them. Ideally, I'd like for t ...

Ember.js - The Veggie Power-Up: [object Object] encountered an error with:

I've recently started using ember and have set up a sandbox to experiment with it. An interesting issue arises when I run the ember s command - an error pops up, but here's the strange part: the error only occurs when I have Sublime Text open (!? ...

Avoiding unlimited re-renders when using useEffect() in React - Tips and Strategies

As a new developer, I recently built a chat application using socket io. In my code, I have the useEffect hook set to only change when the socket changes. However, I also have setMessage within the body of useEffect(), with socket as a dependency. Unfortun ...

Is there a better method to accomplish this task in a more effective manner?

Is there a more efficient way to achieve this code with fewer lines of code? I'm looking for a solution that avoids repetition and makes it easier to manage, especially since I plan on creating multiple instances of this. Performance is a key consider ...

What is the best way to activate multiple events within an overlapping area containing multiple divs at the same

How can I trigger events on same level divs that overlap in different areas? When there are multiple divs overlapping, only one of them gets triggered. Is there a way to trigger events on all overlapped same level divs? Here is an example code snippet: ...

The AngularJS DOM seems to be updating, but the screen is not reflecting any changes. Uncertain about the next steps to take

I am experiencing a perplexing issue where, despite ng-click updating the expected scope variables and the changes being visible in the DOM via the Chrome debugger, the altered DOM elements are not redrawn in the browser. This anomaly occurs consistently o ...

Getting latitude and longitude from Google Maps in a React Native appHere are the steps to

Looking to retrieve the latitude and longitude from Google using React Native. As a newcomer to React Native, I have been unable to find any resources to address this issue. Any assistance would be greatly appreciated. Thanks! Thanks ...

Passing JSON Data Between Functions within an Angular Controller

In my current setup using Node.js, Angular, Express, and HTML, I have a text area that gets filled with formatted text data for letters when certain buttons are clicked. <div class="form-group"> <label class="control-label" for="flyer descriptio ...

Is there a way to verify the existence of an Array Field?

For JavaScript, the code would look like this: if (array[x] != undefined && array[x][y] != undefined) { array[x][y] = "foo"; } Is there an equivalent simple method for achieving this in Java? I have tried checking if the field is null or no ...

The jqGrid displaying data with datatype set to "jsonstring" is only showing the first page

When my JqGrid receives data from the server in JSON format, I use the following parameters: _self.GridParams = { rows: 7, page: 1, sidx: '', sord: '' } Once the data is retrieved, it is stored in the variable 'data': Objec ...

Validating date and time picker pairs using JavaScript

I am looking to implement start and end date validation using datetimepicker. My current code is not functioning as expected. I need the end date to be greater than or equal to the start date, and vice versa. If anyone has a solution for this issue, plea ...

Is there a way to conceal the contents of a page until all the images have finished loading?

I'm currently working on improving the performance of a website that is loading very slowly. I have already reorganized, compressed and minified the JavaScript and CSS files, but the main issue seems to be with the images. The site contains large imag ...

You cannot add properties to an object within an async function

I am attempting to include a fileUrl property in the order object within an async function, but I am unable to make it work properly. My expectation is for the order object to contain a fileUrl property once added, but unfortunately, it does not seem to b ...

The behavior of Elementor lightbox buttons upon being clicked

When using Android, I've noticed that the lightbox briefly displays a semitransparent cyan bar on the left and right buttons when they are pressed. Is there a way to control or prevent this behavior? Any suggestions would be appreciated! Thanks in adv ...