Angularjs 将HTTP传递到Angular 2服务

Angularjs 将HTTP传递到Angular 2服务,angularjs,unit-testing,karma-jasmine,angular2-services,ngrx,Angularjs,Unit Testing,Karma Jasmine,Angular2 Services,Ngrx,我当前收到一个错误,该错误表示无法读取null的属性“get”,这是因为我将null作为第一个参数传递给规范文件beforeach中的FirmService构造函数……在这里模拟或传递http到我的服务的最佳方式是什么 @Injectable export class FirmService { public stateObservable: Observable<FirmState>; constructor(private: $http: AuthHttp, private

我当前收到一个错误,该错误表示无法读取null的属性“get”,这是因为我将null作为第一个参数传递给规范文件beforeach中的FirmService构造函数……在这里模拟或传递http到我的服务的最佳方式是什么

@Injectable
export class FirmService {
 public stateObservable: Observable<FirmState>;

 constructor(private: $http: AuthHttp, private store: Store<FirmState>) {
    this.stateObservable = this.store.select('firmReducer');

  }

  public getFirms(value?: string) {
     return this.$http.get('/api/firm').map((response: Response) => {
           this.store.dispatch({
               type: firmActions.GET_FIRMS,
               payload: response.json()
            });
            return;
     }
  }
}
以下是我对上述服务的单元测试:

import {Store} from '@ngrx/store';
import {FirmService} from './firm.service'
import {firmActions} from './firm.reducer'
import {FirmState} from './firm.state'
import {HttpModule, Http, Response, ResponseOptions, XHRBackend} from 'angular/http';
import {MockBackend, MockConnection} from '@angular/http/testing';

class MockStore extends Store<FirmState> {
    constructor() {
        super(null, null, null)
    }

    public dispatch () {
         return undefined;
     }
}

describe('firm actions', () => {
     it('getFirms should dispatch the GET_FIRMS action', () => {
          let connection: MockConnection;
          const expectedAction = {
             type: firmActions.GET_FIRMS
             payload: undefined
          }

 const mockBackendResponse = (connection: MockConnection, response: string) => {
  connection.mockRespond(new Response(new ResponseOptions({ body: response })));

      TestBed.configureTestingModule({
           imports: [HttpModule],
           providers: [
               {provide: XHRBackend, useClass: MockBackend}
           ]
       });

    spyOn(mockStore, 'dispatch');
    firmService.getFirms().subscribe(result => {
       expect(mockStore.dispatch).toHaveBeenCalled();
       expect(mockStore.dispatch).toHaveBeenCalledWith(expectedAction);
    };
  }
 }
} 

您可以使用angular的http/测试库中的MockBackend和MockConnection尝试以下操作:

import { ResponseOptions, Response, XHRBackend, HttpModule } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';

const mockBackendResponse = (connection: MockConnection, response: string) => {
  connection.mockRespond(new Response(new ResponseOptions({ body: response })));
};

// test module configuration for each test
const testModuleConfig = () => {
  TestBed.configureTestingModule({
    imports: [
      //.. your required modules for this test,
      HttpModule, RouterTestingModule
    ],
    providers: [
      // required services,
      { provide: XHRBackend, useClass: MockBackend }
    ]
  });
};
然后在每次测试之前:

beforeEach(() => {
      injector = getTestBed();
      backend = <any>injector.get(XHRBackend);
      store = injector.get(Store);
      // sets the connection when someone tries to access the backend with an xhr request
      backend.connections.subscribe((c: MockConnection) => connection = c);

      // construct after setting up connections above
        firmService = injector.get(FirmService);
    });
使用项目数组作为结果的样本测试:

t.it('should search', () => {

let list: Array<Item> = []; // ... your sample mock entity with fields

      observer.subscribe(result => {
        expect(result).toEqual(new SearchedAction(list));
      });

      // mock response after the xhr request (which happens in constructor), otherwise it will be undefined
      let expectedJSON:string = JSON.stringify(list);
      mockBackendResponse(connection, expectedJSON);
}

可能是这个私有的:$http,AutHttp你有一个comma@Nello,对不起,我在帖子中做了更正。我基本上是想了解从规范文件中满足getFirms函数中http调用的最佳方法