Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

How to mock a service which has http requests?

Đang mở
#59 2 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
25/100
Loại issue
Tài liệu
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
typescript
Lĩnh vực
api, testing-qa

Hướng nghiên cứu

Issue này nêu tên service.ts và service.spec.ts, đồng thời tập trung vào việc getGoogle() gọi http.get(). Hãy bắt đầu bằng cách xem xét thiết lập Karma và Jasmine của repository cũng như cấu hình kiểm thử hiện có. Công việc được xem là hoàn tất khi có một cách được lập tài liệu và có thể tái lập để kiểm thử phương thức mà không phụ thuộc vào request trực tiếp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Hey,
I know this is not the right platform to ask this, but on right platform, there is no reply from anyone.
How to mock a service which contains http requests?
For eg:

I want to test my provider class, so I am unable to write spec for providers.
My provider is as follows:
service.ts

//imports are done correctly.  
@Injectable() 
export class Service {     
constructor(private http: Http) { }      
getGoogle():Observable<any> {        
 console.log("Inside service");       
  return this.http.get('https://jsonplaceholder.typicode.com/posts/1');   
  } }

My page.ts is as follows:

page.ts

**//imports are done correctly.

  export class Page1 {  
  constructor(private service: Service, private navCtrl: NavController) { }   
async get() {     console.log("inside get method");     
const data =  await this.service.getGoogle().toPromise();       
console.log('The response is' , data);       
 } } 

service.spec.ts

//imports are done correctly  

describe('Service', () => 
{     let comp: Service;    
 let fixture: ComponentFixture<Service>;    
 let de: DebugElement;      
 beforeEach(async(() => {         
TestBed.configureTestingModule({             
declarations: [],             
imports: [               
 IonicModule.forRoot(Service)           
   ],            
 providers: [Http]       

  }).compileComponents();   

   }));     

beforeEach(() => {        

 fixture = TestBed.createComponent(Service);      
  comp = fixture.componentInstance;         
 de = fixture.debugElement;   
  });    

 afterEach(() => {       

  fixture.destroy();    

 }); 

it('test the http request to te server', ()=>{           

   //code to test http request of the Service class  }); 

 });** 

All the imports are done correctly, only the logic to test the getGoogle() with http.get() needs to be tested.Please help or share some links or tell me some steps in order to do testing of this ionic2 content.

Thanks
Adi

Ngôn ngữ chính
TypeScript
Star
370
Fork
144
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ionic-team/ionic-unit-testing-example

Tất cả issue của ionic-team/ionic-unit-testing-example

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.