0

Has anyone been able to build specs or e2e tests using $httpBackend while having BreezeJS function as your entity manager?

Here is the BreezeJS code:

function getClientsAll() {
    var query = breeze.EntityQuery.from('clients').using($clientJsonResultsAdapter);        
    return em.executeQuery(query);
}

And here is the test code from the Jasmine Spec using AngularJS $httpBackend:

it('should show all clients', function() {
    $httpBackend.expectGET(/clients/).respond([ 200, mockData.clients.GET]);
    backupModelService.getClientsAll()
        .then(function() {
            console.log("getClientsAll success");
        })
        .fail(function() {
            console.log("getClientsAll fail");
        })
    $httpBackend.flush();//error thrown here
});

But I get the jasmine test failure

Error: No pending request to flush !

This breeze method works fine running in the browser but not during tests. However, if I convert to using angularJS $http or $resource, this test will pass. I'm wondering if BreezeJS is not compatible with the $httpBackend for spec and e2e testing.

Thanks for any insight.

//EDIT --- AFTER TRYING TO MOCK AJAX BECAUSE OF STEVE'S RESPONSE

This fails as well.

    var success = jasmine.createSpy('success');
    var fail = jasmine.createSpy('fail');
    spyOn($, 'ajax').andCallFake(function (req) {
        var d = $.Deferred();
        d.resolve(mockData.clients.GET);
        return d.promise();
    });
    backupModelService.getClientsAll()
        .then(success)
        .fail(fail);

    expect(success).toHaveBeenCalled();
4

2 回答 2

1

Breeze 不使用 Angular 的 $http 或 $resource 来发送 XHR。它使用 jQuery 的 AJAX 实现。因此使用执行的查询em.executeQuery(query)不会通过 $httpBackend。

使用 Breeze 进行测试时,您应该监视/存根/模拟 jQuery 的 $.ajax,如本文所述。

于 2013-09-06T20:52:48.747 回答
0

这并不完全正确。Breeze 在 v.1.4.4 中为 $http 添加了一个 ajax 适配器 这是设置 ajax 适配器的方法

function configBreeze($q, $http) {
    // tell the adapter to use your app module's $http for ajax calls

    var ajax = breeze.config.initializeAdapterInstance('ajax', 'angular');
    ajax.setHttp($http);
}

在此之后,您将能够在测试中使用 $httpBackend

更多信息在这里 http://www.breezejs.com/documentation/customizing-ajax 在这里 微风使用角度 $http 拦截器

于 2014-02-05T13:59:10.873 回答