如何实际重置$httpBackend期望
How to actually reset $httpBackend expectations?
我已经试过了,试过了。文档很简洁,最多:
resetExpectations(); -重置所有请求期望,但保留所有后端定义。通常,当您希望重用相同的$httpBackend mock实例时,您将在多阶段测试中调用resetExpectations。
每次调用第二个请求时,结果总是具有第一个结果的数据。查看这个fiddle http://jsfiddle.net/tbwn1gt0/2/,我在第一次刷新后重置期望,然后设置新的期望/结果,然后再次刷新以产生不正确的数据。
// --- SPECS -------------------------
var url = '/path/to/resource';
var result = '';
describe('$httpBackend', function () {
it("expects GET different results in subsequent requests", inject(function ($http, $httpBackend) {
successCallback = function(data){
result = data;
}
// Create expectation
$httpBackend.expectGET(url).respond(200, 'mock data');
// Call http service
$http.get(url).success(successCallback);
// flush response
$httpBackend.flush();
console.log( result ); // logs 'mock data'
// Verify expectations
expect( result ).toContain('mock data'); // works as it should
// reset the expectations
$httpBackend.resetExpectations();
// set the fake data AGAIN
$httpBackend.expectGET(url).respond(200, 'doof the magic cragwagon');
// get the service AGAIN
$http.get(url).success(successCallback);
expect( result ).toContain('doof'); // does not work, result is original result
console.log( result ); // logs 'mock data'
}));
});
// --- Runner -------------------------
(function () {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function (spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function () {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
我尝试过的其他事情包括添加一个带有resetExpectations的afterEach(将每个请求放在一个新的it语句中)。还有很多其他的随机尝试。如果它试图将预期的url更改为不期望的东西,它应该出错——所以我知道请求至少通过httpBackend处理。
这是一个缺陷还是我实现它不正确?
.resetExpectations()
确实如您所期望的那样工作,但您只是忘记为第二个刷新http请求。
// set the fake data AGAIN
$httpBackend.expectGET(url).respond(200, 'doof the magic cragwagon');
// get the service AGAIN
$http.get(url).success(successCallback);
$httpBackend.flush(); // flush the second http request here
expect( result ).toContain('doof'); // does not work, result is original result
console.log( result ); // logs 'mock data'
示例JSFiddle: http://jsfiddle.net/4aw0twjf/
p。实际上,$httpBackend.resetExpectations()
对于您的测试用例是不必要的。
您的案例不需要重置期望。你可以修改期望行为,而不是清除&创建一个新的。"expectGET"(和其他方法)返回requestHandler,例如:
// Create expectation
var expectationHandler = $httpBackend.expectGET(url).respond(200, 'mock data');
// Call http service, flush response, verify expectations
// Modify behavior
expectationHandler.respond(200, 'doof the magic cragwagon');
// Call http service ... AGAIN
由于您有多个调用,我将在每个测试结束时使用它来验证所有http调用都是成功的expect($httpBackend.flush).not.toThrow();
,这意味着您不必为每个http调用调用$httpBackend.flush();
。
同时,你也可以像这样添加一个afterEach
块。
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectations();
});
- 为什么jasmine期望不验证是否抛出了错误
- Facebook:当发布期望对象引用时显示打开的图形对话框
- httpBackend模拟json文件响应
- React+Redux-期望reducer是一个函数
- 如何检查$httpBackend中是否未进行GET调用
- 如何用备用模拟服务器替换$httpBackend
- “期望赋值或函数调用,而是看到了表达式.” - 不必要的控制台.log
- 地图(int,int)期望由帖子请求
- Chai 期望 [函数] 抛出一个(错误)未通过测试(使用 Node)
- 这是链接chai.js's期望在守夜
- Js提示:“;期望一个赋值或函数调用,而看到一个表达式“;,Switch语句
- 单元测试接收到带有$httpBackend的二进制响应
- 获得正确的数据,但不是我期望的数据
- 显示Jasmine运行的测试/期望的总数
- IE10 Javascript”;期望函数”;
- Angular-使用httpBackend测试http服务会引发意外异常
- 函数在我期望长度为 17 的地图时返回空
- 如何使用 HttpBackend 进行$http
- 如何实际重置$httpBackend期望
- $httpBackend.期望与$httpBackend.何时