zoukankan      html  css  js  c++  java
  • [Angular Unit Testing] Testing Services with dependencies

    import { Http, Response, ResponseOptions } from '@angular/http';
    import { TestBed } from '@angular/core/testing';
    import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
    
    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/of';
    
    import { StockInventoryService } from './stock-inventory.service';
    
    TestBed.initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
    );
    
    function createResponse(body) {
      return Observable.of(
        new Response(new ResponseOptions({ body: JSON.stringify(body) }))
      );
    }
    
    class MockHttp {
      get() {
        return createResponse([]);
      }
    }
    
    const cartItems = [{ product_id: 1, quantity: 10 }, { product_id: 2, quantity: 5 }];
    const productItems = [{ id: 1, price: 10, name: 'Test' }, { id: 2, price: 100, name: 'Another Test' }];
    
    describe('StockInventoryService', () => {
    
      let service: StockInventoryService;
      let http: Http;
    
      beforeEach(() => {
        const bed = TestBed.configureTestingModule({
          providers: [
            StockInventoryService,
            { provide: Http, useClass: MockHttp }
          ]
        });
        http = bed.get(Http);
        service = bed.get(StockInventoryService);
      });
    
      it('should get cart items', () => {
        // [...cartItems]: do a copy
        spyOn(http, 'get').and.returnValue(createResponse([...cartItems]));
    
        service.getCartItems()
          .subscribe((result) => {
            expect(result.length).toBe(2);
            expect(result).toEqual(cartItems);
          });
      });
    
      it('should get product items', () => {
        spyOn(http, 'get').and.returnValue(createResponse([...productItems]));
    
        service.getProducts()
          .subscribe((result) => {
            expect(result.length).toBe(2);
            expect(result).toEqual(productItems);
          });
      });
    
    });
  • 相关阅读:
    highcharts
    iCheck
    MdiContainer
    wms-ssv数据字典
    hibernate 返回自定义对象
    XmlSerialize
    Db
    python groupby
    pom resource配置
    FastReport打印table
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6801901.html
Copyright © 2011-2022 走看看