Javascript 茉莉花没有承诺

Javascript 茉莉花没有承诺,javascript,angularjs,jasmine,karma-jasmine,Javascript,Angularjs,Jasmine,Karma Jasmine,我不认识茉莉花。我试图让我的工厂承诺得到解决,但它似乎从来没有解决 这是我的工厂,它将收回一份公司名单 还有我的规格 正如您所看到的,我使用$rootScope.$digest来运行promise,但是我的期望从未达到,因此测试总是通过 我读过很多关于这方面的帖子,他们都说这应该行得通。我是做错了什么,还是错过了什么 编辑-简化方法 因此,在看了更多的帖子后,我简化了我的问题 这是我的新控制器呼叫 $scope.companies = []; var GetCompani

我不认识茉莉花。我试图让我的工厂承诺得到解决,但它似乎从来没有解决

这是我的工厂,它将收回一份公司名单

还有我的规格

正如您所看到的,我使用$rootScope.$digest来运行promise,但是我的期望从未达到,因此测试总是通过

我读过很多关于这方面的帖子,他们都说这应该行得通。我是做错了什么,还是错过了什么

编辑-简化方法 因此,在看了更多的帖子后,我简化了我的问题

这是我的新控制器呼叫

        $scope.companies = [];
    var GetCompanies = function(options) {
        companyFactory.GetCompanies().then(function(response) {
            options.success(response);
            $scope.companies = response
        });
    }
$scope.GetCompanies = GetCompanies
我的工厂

function getCompanies() {
    //call service and include company identifier in the service url
    return $http.get(wunUtils.constants.serviceBaseUrl + 'CompanyService.svc/companies')
        .then(function(response) {
            return response.data;
        }, function(err){
            console.log(err);
        });
}
    var service = {
    AddCompany: addCompany,
    GetCompany: getCompany,
    GetCompanies: getCompanies,
    GetUserCompanyFleets: getUserCompanyFleets,
    EditCompany: editCompany
};
还有我的新规格

describe('company', function() {
beforeEach(module('mockedCommercial'));

var $httpBackend, companyFactory, $rootScope, createController, scope

beforeEach(function() {

    inject(function($controller, _$httpBackend_, _$rootScope_, _companyFactory_) {
        $rootScope = _$rootScope_;
        $httpBackend = _$httpBackend_;
        scope = _$rootScope_.$new();
        companyFactory = _companyFactory_;
        createController = function() {
            return $controller('ManageCompaniesController', {
                '$scope': scope
            });
        };
    });
})


it('should return available languages asynchronously', function(done) {

    $httpBackend.whenGET(/https:\/\/myDomain.com\/V3_Fleet\/CompanyService.svc\/companies+/)

        .respond(function(method, url, data, headers, params) {
                return [{ 'id': 1 }];
            }
        });

    //--------------------------
    var dummy = [{ 'id': 1 }];

    createController();

    scope.GetCompanies()

    $httpBackend.flush()


    expect(scope.companies).toEqual(dummy)


    scope.$digest(); done()

});

}))

在评估测试预期之前,请尝试使用
$httpBackend.flush()


这是angular建议的方法。有关更多详细信息和示例,您可以查看。

在测试异步代码时,您有两个选项

1.对所有内容使用同步模拟 这包括:

  • 模拟
    $httpBackend
  • 使用
    $timeout.flush()
  • 使用
    $interval.flush()
  • 使用
    scope.$apply()
    处理剩余承诺
在所有这些之后,您仍然不能完全确定所有未决任务是否都已得到处理,尽管它在99.9%的时间内就足够了

2.使您的测试异步化 在这里,您必须确保每个异步控制器方法都返回一个承诺,即使返回的承诺没有在应用程序中使用。它们将用于测试

Jasmine使异步测试变得相当困难,但并非不可能。您必须使用,并对每个异步块进行如下编码:

it('should...', function(done) {
    $q.when().then(function() {
        return $controller.GetSomething();
    }).then(function() {
        expect(scope.companies).toEqual(dummy);
    }).then(done, done.fail);
});
因此,您可以在不完成
的情况下编写

it('should...', function() {
    return $q.when().then(function() {
        return $controller.GetSomething();
    }).then(function() {
        expect(scope.companies).toEqual(dummy);
    });
});
如果您有工具集,甚至可以使用
async wait

it('should...', async () => {
    await $controller.GetSomething();
    expect(scope.companies).toEqual(dummy);
});

因此,经过大量阅读,我通过简化所有内容找到了答案。我认为我的问题来自于混合了$httpBackend.flush()done()函数

似乎发生了这样的情况:done()函数触发它自己的摘要,因此当尝试使用flush()函数时,会出现冲突

这是我的工作工厂。它与我最初的purly有点不同,因为这是一个更简单的函数(出错的事情更少)

下面是我的简化规范$httpBackend.Flush()

编辑

还应注意的是,在测试HTTPBackend时,测试仅在使用expectGET和expectPOST而不是whenGET和whenPOST时有效


从我所看到的,whenGet用于模拟数据,而expectGet用于测试模拟

您解决问题了吗?您的
公司工厂
中存在语法问题。您只是将
getcompanys
声明为本地函数,而不是返回对象的方法。您是否需要
it
回调中返回承诺?请在
$httpBackend.flush()之后查看我的editsTry调用
范围。$apply()
要处理angular.hey@boyomarinov中排队的微任务,我尝试过这个方法,但仍然无法解决。请检查我的问题的编辑
it('should...', function() {
    return $q.when().then(function() {
        return $controller.GetSomething();
    }).then(function() {
        expect(scope.companies).toEqual(dummy);
    });
});
it('should...', async () => {
    await $controller.GetSomething();
    expect(scope.companies).toEqual(dummy);
});
        function getUser(userId) {
        //call service and include user identifier in the service url
        return $http.get(wunUtils.constants.serviceBaseUrl + 'myService.svc/fleetusers/' + userId)
            .then(function(response) {
                return response.data;
            });
         }

        var service = {
        GetMe: getMe,
        GetUser: getUser,
        AddUser: addUser,
        EditUser: editUser,
        ActivateUser: activateUser
    };
describe('Testing httpBackend', function() {

var $httpBackend;

beforeEach(module('mockedCommercial'));


beforeEach(inject(function(_$httpBackend_) {
    $httpBackend = _$httpBackend_;
}))

it('should behave...', function() {

    inject(function(userFactory, $rootScope) {

        var mockData = [];

        $httpBackend.expectGET(wunUtils.constants.serviceBaseUrl + 'FleetUserService.svc/fleetusers/10').respond(mockData);

        var promise = userFactory.GetUser(10);

        $httpBackend.flush();

        promise.then(function(result) {
            expect(result).toEqual(mockData);
        });

        });

});
});