Last active
December 13, 2018 09:22
-
-
Save rorymcgit/819f1a3eb697c864fcf47c9e6f8f5fe2 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', | |
}); | |
// required for slower CI environment | |
setTimeout(() => { | |
done(); | |
}, 2000); | |
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