问题描述
我试过,并试图得到这个工作。该是简洁的,充其量:
I've tried and tried to get this to work. The documentation is terse, at best:
resetExpectations(); - 将所有的请求的期望,但preserves所有后端定义。通常情况下,你会多阶段测试中,当你想重用$同一实例调用resetExpectations httpBackend模拟。
resetExpectations(); - Resets all request expectations, but preserves all backend definitions. Typically, you would call resetExpectations during a multiple-phase test when you want to reuse the same instance of $httpBackend mock.
每一个我的第二个请求被调用时,我的成绩总是第一个结果的数据。看看这个小提琴,我重置后的期望第一次冲水,然后设置新的期望/结果然后再冲洗,产生不正确的数据。
Every time my second request is called, my result always has the first result's data. Check out this fiddle http://jsfiddle.net/tbwn1gt0/2/ where I reset the expectations after the first flush, then set new expectations/result then flush again to yield the incorrect data.
// --- 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(投入新的IT陈述每个请求)的afterEach。等随意尝试的转换。如果试图改变预期的URL的东西不是预期的,它的错误,因为它should--所以我知道的要求越来越至少是通过httpBackend处理。
Other things I've tried include adding an afterEach with resetExpectations (putting each request in a new it statement). and a slew of other random attempts. If it try to change the expected url to something not expected, it errors as it should-- so I know the requests are getting handled through httpBackend at the very least.
这是一个缺陷还是我错误地执行了吗?
Is this a defect or am I implementing it incorrectly?
推荐答案
的 .resetExpectations()
做的工作如你预期,但你只是忘了冲件的http请求对于第二个
The .resetExpectations()
does work as you expected, but you just forgot to flush a http request for the second one.
// 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:
PS。其实, $ httpBackend.resetExpectations()
没有必要为你的测试用例。
PS. Actually, the $httpBackend.resetExpectations()
is not necessary for your test case.
这篇关于如何真正重置$ httpBackend期望?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!