Skip to content

Instantly share code, notes, and snippets.

@rorymcgit
Created December 19, 2018 10:06
Show Gist options
  • Save rorymcgit/cf7cc8f07c20507f699900f8484435c1 to your computer and use it in GitHub Desktop.
Save rorymcgit/cf7cc8f07c20507f699900f8484435c1 to your computer and use it in GitHub Desktop.
import { ApiService } from './api.service';
import { HttpClientModule } from '@angular/common/http';
import { Matchers, PactWeb } from '@pact-foundation/pact-web';
import { TestBed } from '@angular/core/testing';
describe('PACT ApiService', () => {
let apiService: ApiService;
let provider: PactWeb;
const dummyBodyData = {
foo: 'bar',
};
beforeAll(done => {
provider = new PactWeb({
consumer: 'frontend',
provider: 'backend',
port: 1234,
host: '127.0.0.1',
});
provider.removeInteractions().then(done, e => done.fail(e));
});
afterAll(done => {
provider.finalize().then(done, e => done.fail(e));
});
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule,
],
providers: [
ApiService,
],
});
apiService = TestBed.get(ApiService);
});
describe('getFoo', () => {
beforeAll(done => {
provider.addInteraction({
state: 'provider responds with foo',
uponReceiving: 'a request to GET foo',
withRequest: {
method: 'GET',
path: '/foo',
},
willRespondWith: {
status: 200,
body: Matchers.somethingLike((dummyBodyData)),
headers: {
'Content-Type': 'application/json',
},
},
}).then(done, e => done.fail(e));
});
it('provider responds with foo upon receiving a request to GET foo', done => {
apiService.getFoo().subscribe(response => {
expect(response).toEqual(dummyBodyData);
provider.verify().then(done, e => done.fail(e));
}, err => {
console.log('Test error: ', err.error.message);
done.fail(err);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment