Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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 在单元测试中,当请求之间出现[method]语句时,如何更改$httpBackend?_Javascript_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

Javascript 在单元测试中,当请求之间出现[method]语句时,如何更改$httpBackend?

Javascript 在单元测试中,当请求之间出现[method]语句时,如何更改$httpBackend?,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,在测试中,我启动一些模型数据并模拟响应: beforeEach(function(){ var re = new RegExp(/^http\:\/\/.+?\/users-online\/(.+)$/); $httpBackend.whenGET(re).respond({id:12345, usersOnline:5000}); }); it('should find 5000 users in channel 12345', function(){ exp

在测试中,我启动一些模型数据并模拟响应:

beforeEach(function(){
   var re = new RegExp(/^http\:\/\/.+?\/users-online\/(.+)$/);
   $httpBackend.whenGET(re).respond({id:12345, usersOnline:5000});
});      

it('should find 5000 users in channel 12345', function(){
    expect( UsersOnlineService.data.usersOnline).toEqual( 50000);
});
然后在下一个测试语句中说,我想要通道的更新值。每个人都离开了,假设这会触发其他行为,所以我需要模拟第二个请求,以便测试该行为

$httpBackend.whenGET
添加到下一个
it
station不会覆盖每个
之前的
值。它似乎使用了原始的模拟值:

it('should find 0 users in channel 12345', function(){
    $httpBackend.whenGET(re).respond({id:12345, usersOnline:0});
    expect( UsersOnlineService.data.usersOnline).toEqual( 50000); //fails
});
如果我这样做而不使用beforeach,两者都会失败,出现通常的“意外”错误

如何在请求之间调整模拟数据

我还尝试:

  • 在每个
之前将一个
夹在
it
语句之间
  • 设置
    。响应(
    fakeResponse
    变量。然后更改每个
    it
    语句中的
    fakeResponse
  • 重置期望值() 执行

    afterEach($httpBackend.resetExpectations);
    
    文件

    重置所有请求预期,但保留所有后端定义。通常,当您希望重用$httpBackend mock的同一实例时,您会在多阶段测试期间调用ResetExpections


    文档来源:$httpBackend

    我没有看到任何发出http请求的内容。您必须在发出请求之前设置
    $httpBackend
    。啊,我在某个时候尝试过这一点,然后我意识到我需要使用expect('GET',regex)。respond()不是whenGET(…)事实上,我欺骗自己认为这是有效的。我仍然没有运气。我用一个例子JSFIDLE提出了一个新问题,现在我对这个问题有了更多的了解:
    afterEach($httpBackend.resetExpectations);