【发布时间】:2016-01-11 20:54:59
【问题描述】:
我不完全确定如何执行此操作,但我有一个端点 URL,它是用于登录身份验证的 POST 请求。添加请求负载时,您将获得成功的登录凭据或错误。但是,我似乎在获取响应时遇到了问题。
这是我的 spec 文件:
describe('Service: AuthFactory',function(){
beforeEach(function () {
module('ui.router');
module('users');
module('main');
});
var AuthFactory, httpBackend;
beforeEach(inject(function($httpBackend, $rootScope, $controller, _AuthFactory_){
httpBackend = $httpBackend;
AuthFactory = _AuthFactory_;
}));
it('Return a POST response from a Service function', function() {
var url = "http://localhost:3000";
var dataObj = JSON.stringify({
inputUser: { user: "TestCase1" },
inputPass: { password: "TestPass1" }
});
httpBackend.expect('POST', url + '/api/AuthService/signIn', dataObj)
.respond({});
AuthFactory.signIn(dataObj).success(function(response) {
console.log(response);
// outputs Object {}
// when in reality it should
// output the response to POST
// eg: { "login": { "user": "Test_User", "time": 54935934593 }, "outputHead": { "token": asjfjj234kfAd } }
});
httpBackend.flush();
expect(true).toBe(true);
});
});
这是我的Service。
angular.module('users').factory('AuthFactory', ['$http', function($http) {
var AuthFactory = {};
AuthFactory.signIn = function(data) {
return $http.post('http://127.0.0.1:3000/api/AuthService/signIn', data);
};
AuthFactory.signOut = function(data) {
return $http.post('http://127.0.0.1:3000/api/AuthService/signOut', data);
};
return AuthFactory;
}]);
当我运行测试时,它(显然)通过了,但 console.log() 输出 Object{}。
当我使用像 Postman 这样的 Chrome 扩展程序时。我做了一个示例 POST 请求,返回的响应是登录凭据!那么为什么它可以在 Postman 上运行,但在我的 AngularJS Jasmine 单元测试上却不行呢?
【问题讨论】:
标签: javascript angularjs unit-testing jasmine