意外请求:POST /api/v1/auth/sign_in 预期 POST http://localhost:3000/api/v1/auth/sign_in

Unexpected request: POST /api/v1/auth/sign_in Expected POST http://localhost:3000/api/v1/auth/sign_in

我正在为登录工厂编写一个测试用例(在 jasmine 中,运行用 karma 对其进行测试),但每次我尝试 运行 时都会抛出 'Unexpected request: POST /api/v1/auth/sign_in Expected POST http://localhost:3000/api/v1/auth/sign_in' 错误测试。

我的工厂代码如下:

app.factory('signUpFactory', function($http, baseUrl, access_token){
  // authUrl = baseUrl + "/auth";
  authUrl = "/api/v1/auth";
  var signup_user = {};
  var headers = { 'Authorization': 'Token token="' + access_token + '"' };
//Sign in
  signup_user.signIn = function(formData){
    var dataObj = {
      login_id: formData.email,
      password:formData.password,
      login_type: formData.login_type
    };
    return $http.post(authUrl+"/sign_in" , dataObj, {headers: headers});
  };

  return signup_user;
});

对应的测试用例是:

(function() {
  'use strict';

  describe('Registrations Factory: signUpFactory', function(){
    var signUpFactory;
    var $httpBackend,
        response;

    var loginForm = {};

    beforeEach(module('abc'));

    beforeEach(inject(function(_signUpFactory_, _$httpBackend_){
      signUpFactory = _signUpFactory_;
      $httpBackend = _$httpBackend_;
    }));

    it('should sign in if valid params are present', function(){
      var dataObj = {
        login_id: 'email@abc.com',
        password: '123456789',
        login_type: 'Doctor'
      };

      loginForm.email = 'email@abc.com';
      loginForm.password = '123456789';
      loginForm.login_type = 'Doctor';

      $httpBackend.expect('POST','http://localhost:3000/api/v1/auth/sign_in', dataObj).respond("success");

      signUpFactory.signIn(loginForm)
      .then(function(data){
        response = data.status;
      })

      $httpBackend.flush();
      expect(response).toEqual("success");
    })
  })
})();

我搜索了可能的解决方案,但无法修复。将不胜感激。

谢谢!!

您可以尝试使用正则表达式:

 $httpBackend.expect('POST',/.*\/api\/v1\/auth\/sign_in/, dataObj).respond("success");

https://jsfiddle.net/ronapelbaum/e1udbtst/