AngularJS Promise Callback Not Trigged in JasmineJS Test

前端 未结 3 626
挽巷
挽巷 2021-02-05 01:02

Problem Intro

I\'m trying to unit test an AngularJS service that wraps the Facebook JavaScript SDK FB object; however, the test isn\'t working, and I have

3条回答
  •  一整个雨季
    2021-02-05 01:07

        'use strict';
    
    describe('service: Facebook', function () {
        var rootScope, fb;
        beforeEach(module('app.services'));
        // Inject $rootScope here...
        beforeEach(inject(function($rootScope, Facebook){
            rootScope = $rootScope;
            fb = Facebook;
        }));
    
        // And run your apply here
        afterEach(function(){
            rootScope.$apply();
        });
    
        it('should return false if user is not logged into Facebook', function () {
            // Provide a fake version of the Facebook JavaScript SDK `FB` object:
            module(function ($provide) {
                $provide.value('fbsdk', {
                    getLoginStatus: function (callback) { return callback({}); },
                    init: function () {}
                });
            });
            fb.getUserLoginStatus($rootScope).then(function (data) {
                console.log("Found data!");
                expect(data).toBeFalsy(); // user is not logged in
            });
        });
    }); // Service module spec
    

    This should do what you're looking for. By using the beforeEach to set up your rootScope and afterEach to run the apply, you're also making your test easily extendable so you can add a test for if the user is logged in.

提交回复
热议问题