如何实际重置$httpBackend期望

How to actually reset $httpBackend expectations?

本文关键字:httpBackend 期望 何实际      更新时间:2023-09-26

我已经试过了,试过了。文档很简洁,最多:

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();
});