0
我正在創建使用打字稿的角度2應用程序,並需要使用茉莉花測試服務。正如您在測試方法中看到的那樣,我需要向我的服務注入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();
});
});
});
我在這段代碼的useFactory出現錯誤:(backend,options)=> { 返回新的Http(後端,選項); }。它說參數後端隱含有任何類型。相同的選項消息 – Tom
錯誤消息是什麼?看我的編輯。也許你只需要添加一個類型的參數 –
它說參數後端隱含有任何類型。相同的信息選項 – Tom