How to mock a service which has http requests?

オープン
#59 コメント 2 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
25/100
issue の種類
ドキュメント
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
typescript
領域
api, testing-qa

調査の方向性

この issue では service.ts と service.spec.ts が挙げられており、getGoogle() が http.get() を呼び出すことに焦点を当てています。まず、リポジトリの Karma と Jasmine のセットアップ、および既存のテスト設定を確認してください。ライブリクエストに依存せずにメソッドをテストする、文書化された再現可能な方法ができれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
TypeScript
スター
370
フォーク
144
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ionic-team/ionic-unit-testing-example のほかの issue

ionic-team/ionic-unit-testing-example の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。