javascript - 使用 Jasmine 和 AngularJS 未刷新的请求

标签 javascript angularjs karma-jasmine

自从我尝试使用 $httpBackend.verifyNoOustandRequest() 以来,我在通过一些测试时遇到了问题。

如果我不将其包含在我的 afterEach 函数中,那么测试就会通过,但是当我这样做时,除了最后一个“应该从服务器加载项目”之外,它会导致所有测试失败。除了最后一个测试之外,每个失败的测试响应都是“错误:未刷新的请求:1”。

我不明白的是,我认为您只需要在模拟 ajax 请求时担心刷新,但除了我的实际服务器测试之外,它都失败了。

任何帮助将不胜感激。

describe('controller: ListCtrl', function(){
beforeEach(module('notesApp'));
var ctrl, loc, mockService, itemServiceS, mockBackend;
beforeEach(inject(function($controller, $location, ItemService, $httpBackend){
    spyOn(ItemService, 'list').and.returnValue([{id: 1, label: 'First', done: true}]);
    itemService = ItemService;
    mockBackend = $httpBackend;
    mockBackend.expectGET('/api/note').respond([{id: 1, label: 'Mock'}]);
    ctrl = $controller('ListCtrl');         
    loc = $location;
}));
it('Should have items available on load', function(){
    expect(ctrl.items).toEqual([
        {id: 1, label: 'First', done: true},
        {id: 2, label: 'Second', done: false}
    ]);
});
it('Should have highlight items based on state', function(){
    var item = {id: 1, label: 'First', done: true};
    var actualClass = ctrl.getDoneClass(item);
    expect(actualClass.finished).toBeTruthy();
    expect(actualClass.unfinished).toBeFalsy();

    item.done = false;
    actualClass = ctrl.getDoneClass(item);
    expect(actualClass.finished).toBeFalsy();
    expect(actualClass.unfinished).toBeTruthy()
});
it('should change the url', function(){
    expect(loc.path()).toEqual('');
    ctrl.navigate1();
    expect(loc.path()).toEqual('/some/where/else');
});

it('Should change the url to /some/where', function(){
    expect(loc.path()).toEqual('');
    ctrl.navigate2();
    expect(loc.path()).toEqual('/some/where');
});
it('Should have called through ItemService factory', function(){
    expect(itemService.list).toHaveBeenCalled();
    expect(itemService.list.calls.count()).toEqual(1);
    expect(ctrl.itemsGet).toEqual([{id: 1, label: 'First', done: true}]);

});
it('Should load items from server', function(){
    expect(ctrl.retrievedItems).toEqual([]);
    mockBackend.flush();
    expect(ctrl.retrievedItems.data).toEqual([{id: 1, label: 'Mock'}]);
});
afterEach(function(){
    mockBackend.verifyNoOutstandingExpectation();
    mockBackend.verifyNoOutstandingRequest();
})

});

最佳答案

没有看到您的 Controller 代码,我无法确定,但似乎模拟 API 请求是作为 Controller 初始化的一部分发出的。因此,总是会发出请求,并且必须始终刷新它以满足 afterEach 中的断言。

$httpBackend 的 expect() 语法非常严格,您可能更喜欢使用 when() 语法,该语法更宽松,更“黑匣子”。您可以阅读 angularJS documentation for $httpBackend 中的一些差异。

关于javascript - 使用 Jasmine 和 AngularJS 未刷新的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32977317/

相关文章:

javascript - ng 路线不工作指令

javascript - 避免用户在本地工作时被注销

javascript - Jasmine spy 报告功能已被调用时,他们还没有

javascript - 请求失败。来自服务器的意外响应。 Sharepoint 2013 在线出现空错误

javascript - native 原型(prototype)与 $.extension()

javascript - 选中 div 而不是其中的文本

javascript - Puppeteer 无法单击 DOM 中 XPath 指定的元素

jasmine - 是否可以使用 karma 运行并行测试

angularjs - 将 Angular 2.21 升级到 2.25 时,Jasmine httpBackend 相关测试中断

javascript - Angular 2 : How to fade out box-msg after a while which is displayed onClick