错误:对于条件“匹配URL:http:// localhost / FinanceAndLegalApi / api / formNotFilled”,预期会有一个匹配请求,但找不到

时间:2019-04-01 11:03:50

标签: angular karma-jasmine

  

错误:对于标准“匹配URL:http://localhost/FinanceAndLegalApi/api/formNotFilled”,预期有一个匹配请求,但找不到。

单元测试的新手,请提供帮助。检查了相关问题,但没有解决方案。请帮忙。

使用Angular7。

我一直坚持这个错误很长时间。请帮忙。

const URL1 = 'http://localhost/FinanceAndLegalApi/api/formFilled';
const URL2 = 'http://localhost/FinanceAndLegalApi/api/formNotFilled';
const URL3 = 'http://localhost/FinanceAndLegalApi/api/currentQuarter';

@Injectable({
  providedIn: 'root'
})
export class ReportService {

  constructor(private http: HttpClient) { }

  getPendingEmployeeData() {
    return this.http.get(URL2);
  }

  getQuarter() {
    return this.http.get(URL3);
  }

  fetchData(data) {
    return this.http.post(URL1, data);
  }
}
describe('ReportService', () => {
  let reportService: ReportService;
  let httpTestingController: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [ReportService]
    });
    // Returns a service with the MockBackend so we can test with dummy responses
    reportService = TestBed.get(ReportService);
    // Inject the http service and test controller for each test
    httpTestingController = TestBed.get(HttpTestingController);
  });

  it('should be created', () => {
    const service: ReportService = TestBed.get(ReportService);
    expect(service).toBeTruthy();
  });

  it('should return pending employees data',
  fakeAsync(() => {
    let response = [
      {
        FirstName: 'Divesh',
        LastName: 'Error',
        UserName: 'divsoni',
        EmailId: 'thediveshsoni@gmail.com',
        ManagerId: 'thdiveshsoni@gmail.com'
      }
    ];

    reportService.getPendingEmployeeData();

    const url = 'http://localhost/FinanceAndLegalApi/api/formNotFilled';

     // Expect a call to this URL
    const req = httpTestingController.expectOne(url);
    console.log(req.request.url);
    // Assert that the request is a GET.
    expect(req.request.method).toEqual('GET');

    // Respond with this data when called
    req.flush(response);

    // Call tick whic actually processes te response
    tick();

  }));

  afterEach(() => {
    // After every test, assert that there are no more pending requests.
    httpTestingController.verify();
  });
});

0 个答案:

没有答案