Created
December 19, 2018 10:10
-
-
Save rorymcgit/1b6e223cf6d909a319c19c173041006f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
}); | |
done(); | |
provider.removeInteractions(); | |
}); | |
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