Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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/23.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
Ajax Angular Js-返回Jasmine对象的测试POST请求_Ajax_Angularjs_Jasmine_Karma Jasmine - Fatal编程技术网

Ajax Angular Js-返回Jasmine对象的测试POST请求

Ajax Angular Js-返回Jasmine对象的测试POST请求,ajax,angularjs,jasmine,karma-jasmine,Ajax,Angularjs,Jasmine,Karma Jasmine,我已经建立了一个服务,可以进行所有AJAX调用。我想测试我的登录方法,它向特定的URL发送一个AJAX POST$http.POST,返回一个带有结果的对象(登录通过或失败)。这个结果是一个对象。我还没有返回测试服务的代码,但我正在尝试先测试URL。这就是它现在的样子: 'use strict'; describe('Service: Ajax', function () { var service, httpBackend; // load the service's m

我已经建立了一个服务,可以进行所有AJAX调用。我想测试我的登录方法,它向特定的URL发送一个AJAX POST
$http.POST
,返回一个带有结果的对象(登录通过或失败)。这个结果是一个对象。我还没有返回测试服务的代码,但我正在尝试先测试URL。这就是它现在的样子:

'use strict';

describe('Service: Ajax', function () {

    var service, httpBackend;

    // load the service's module
    beforeEach(module('mySampleApp'));

    // instantiate service
    beforeEach(inject(function (Ajax, _$httpBackend_) {
        httpBackend = _$httpBackend_;
        service = Ajax;
    }));

    it('Test AJAX call', function () {
        httpBackend.expect('POST', 'http://myloginurl', {u: 'xyz', p: 'pass'}, { withCredentials: true})
        .respond(201, 'success');
    });
});
这就过去了。现在我试着输入一个错误的URL,错误的用户名/密码,但还是通过了!我如何测试这个

更新: 现在写得更好:

//Ajax is my AJAX service
it('should test the login AJAX call', inject(function (Ajax) {
    httpBackend.expect('POST', 'http://myloginurl')
        .respond(200, "[{ status: 200, //some more data }]");
    httpBackend.flush();
    Ajax.authenticate({u: 'xyz', password: 'pass' })
        .then(function(data){
            expect(data.status).toBe(200);
        });
}));
我明白了:

PhantomJS 1.9.7 (Linux) Service: Ajax should test the login AJAX call FAILED
Error: No pending request to flush !
blah blah...
你需要放一个

httpBackend.flush();
在这种情况下,如果未调用预期的url,将引发异常,从而导致测试失败

此外,我看不出您在任何地方调用执行Ajax请求的代码-您需要在调用flush()之前执行该操作

比如:

it('Test AJAX call', function () {
        httpBackend.expect('POST', 'http://myloginurl', {u: 'xyz', p: 'pass'}, { withCredentials: true})
        .respond(201, 'success');
        service.functionThatMakesCallToServer();
        httpBackend.flush();
});

如果函数thatMakeScaltServer()调用httpBackend.expect(…)行中的url,则一切都会正常。如果没有,httpBackend.flush()将抛出一个错误,因为预期的调用没有发生。该错误将导致您的测试失败。

我正在更新我的问题并添加得到的回答。请检查一下!另外,
httpBackend.expect(…)
AJAX调用正确吗?不,httpBackend.expect(…)不执行调用-它告诉httpBackend期待调用。然后,httpBackend.flush()处理所有调用。在这两件事之间,您需要调用执行ajax请求的函数。我如何添加它?对不起,我是一个笨蛋,我正在试着去测试
post
AJAX调用!哦,所以我需要包括实际拨打电话的服务?谢谢返回的数据如何,如何检查?