Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Jasmine测试有角度的Factory对象_Javascript_Jquery_Angularjs_Jasmine - Fatal编程技术网

Javascript 如何使用Jasmine测试有角度的Factory对象

Javascript 如何使用Jasmine测试有角度的Factory对象,javascript,jquery,angularjs,jasmine,Javascript,Jquery,Angularjs,Jasmine,我试图解决的问题是使用茉莉花测试我的工厂的能力 以下是我的应用程序和工厂的副本: var app = angular.module('app', []); app.factory('service', function ($http) { return { getCustomers: function (callback) { $http.get('/Home/Customers').success(callback); },

我试图解决的问题是使用茉莉花测试我的工厂的能力

以下是我的应用程序和工厂的副本:

var app = angular.module('app', []);

app.factory('service', function ($http) {
    return {
        getCustomers: function (callback) {
            $http.get('/Home/Customers').success(callback);
        },
        getProfile: function (callback, viewModel) {

            $http.post('/Home/Profiles', JSON.stringify(viewModel), {
                headers: {
                    'Content-Type': 'application/json'
                }
            }).success(callback);
        }
    };
});
:::::::::::::::::::::

我也设置了jasmine,但是我在测试上面的“getCustomers”和“getProfile”时遇到了问题

以下是我目前的尝试:

  describe("getCustomers", function (service) {
      beforeEach(module('service'));
         describe('getCustomers', function () {
            it("should return a list of customers", inject(function(getCustomers){
              expect(getCustomers.results).toEqual(["david", "James", "Sam"]);
         }))
      })
  });
如果有人能提供一个例子,说明如何在两个单独的测试中同时测试“getCustomers”和“getProfile”,这将非常有用


问候您。

您可以模拟Http GET请求并像这样测试服务

describe("getCustomers", function (service) {
    beforeEach(module('app'));

    var service, httpBackend;
    beforeEach(function () {
        angular.mock.inject(function ($injector) {
            httpBackend = $injector.get('$httpBackend');
            service = $injector.get('service');
        })
    });

    describe('getCustomers', function () {
        it("should return a list of customers", inject(function () {
            httpBackend.expectGET('/Home/Customers').respond(['david', 'James', 'Sam']);
            service.getCustomers(function (result) {
                expect(result).toEqual(["david", "James", "Sam"]);
            });
            httpBackend.flush();
        }))
    })
});

您好,感谢您的回复。我仍然收到此错误:getCustomers遇到声明异常。ReferenceError:module未在@Mark中定义。请查看我为您创建的工作演示,您应该能够将其与您的解决方案进行比较。您好,我这样做了,但仍然无法使其工作。似乎与我如何设置spec runner有关。我现在在spec runner中遇到此错误:无法实例化模块myApp,原因是:[$injector:modulerr]