ionic-team / ionic-team/ionic-unit-testing-example

How to mock a service which has http requests?

オープン
#59 コメント 2 件 リアクション 3 件 担当者 0 名 GitHub で見る
主要言語
TypeScript
スター
370
フォーク
144
PR マージ指標
30日以内にマージされた PR はありません

説明

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 {
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;
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

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

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

評価

この issue はまだ評価されていません。

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

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