【问题标题】:Inject in Jasmine注入茉莉花
【发布时间】:2016-12-12 00:13:16
【问题描述】:

我正在使用 typescript 创建一个 angular 2 应用程序,并且需要使用 Jasmine 测试该服务。正如您在测试方法中看到的那样,我需要将 http 注入到我的服务中才能正常工作。如何将 HTTP 注入我的服务。这是我指的代码行

var _getCustomerService: GetCustomerService = new GetCustomerService (http);

服务方式

@Injectable()
export class GetCustomerService {

    constructor(private http: Http) { }

    private customerUrl = 'http://localhost:45870/api/getcustomer';

    getCustomer(): Observable<Customer[]> {

        return this.http.get(this.customerUrl)
            .map((res: Response) => res.json())
            .catch((error: any) => Observable.throw(error.json().error || 'Server Error'));
    }
}

茉莉花测试

describe("Service test", function () {

    it("check service", function () {
         var http: Http;
        var _getCustomerService: GetCustomerService = new GetCustomerService (http);
        _getCustomerService.getCustomer()
            .subscribe(
            (Customer: Customer[]) => {
                var customer: Customer[];
                customer = Customer;
                expect(customer).toBeDefined();
            });


    });

});

【问题讨论】:

    标签: angular jasmine


    【解决方案1】:

    您不想为测试发出真正的 XHR 请求,因此您需要为测试创建一个专门的 on,您可以在其中为每个请求设置模拟响应。为此,您需要使用工厂创建自己的Http,并使用MockBackend

    import { MockBackend, MockConnection } from '@angular/http/testing';
    import { Http, HttpMdodule, XHRBackend, RequestOptions, Response, ResponseOptions
           } from '@angular/http';
    import { TestBed, async } from '@angular/core/testing';
    
    describe('..', () => {
      let backend: MockBackend;
      let service: GetCustomerService;
    
      beforeEach(() => {
        const injector = TestBed.configureTestingModule({
          imports: [HttpModule],
          providers: [
            GetCustomerService,
            RequestOptions,
            MockBackend,
            {
              provide: Http,
              deps: [MockBackend, RequestOptions],
              useFactory: (backend: MockBackend, options: RequestOptions) => {
                return new Http(backend, options);
              }
            }
          ]
        });
    
        backend = injector.get(MockBackend);
        service = injector.get(GetCustomerService);
      });
    });
    

    现在在您的测试中,您想订阅MockBackend 的连接,以便在连接进入时发送响应

    it('..', async(() => {
      backend.connections.subscribe((conn: MockConnection) => {
        conn.mockRespond(new Response(new ResponseOptions({ body: 'some body ' })));
      });
    
      service.getCustomer().subscribe((Customer: Customer[]) => {
        expect(customer).toBeDefined();
      });
    }));
    

    另请参阅:

    【讨论】:

    • 我在这行代码中遇到错误 useFactory: (backend, options) => { return new Http(backend, options); } 。它说参数后端隐含具有任何类型。选项的相同消息
    • 错误信息是什么?请参阅我的编辑。也许你只需要在参数中添加一个类型
    • 它表示参数后端隐含任何类型。选项的相同消息
    • 是的,你只需要输入它
    • 我不明白。你能解释一下
    猜你喜欢
    • 2018-06-26
    • 2016-05-28
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多