监视一个不会回叫终端服务的 Angular 服务

function calculatorService() {
    const service = {};
    service.add = function(a,b) {
        return a + b
    }        

    return service;
}

angular.module('app').factory('calculatorService', calculatorService);

测试

describe('calculator service', function() {
    var calculatorService;
    beforeEach(angular.mock.module('app'));
    
    beforeEach(inject(function(_calculatorService_) {
         calculatorService = _calculatorService_;
    }));

    it('should should add two numbers', function(){
        var actual = calculatorService.add(1,2);
        expect(actual).toBe(3);
    });
})