Last active
January 8, 2019 15:20
-
-
Save rorymcgit/7858c2e82f4d70341072d981dc70b54b 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 dummyResponse = { | |
foo: Matchers.eachLike({ | |
bar: ['a string'], | |
}), | |
}; | |
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: dummyResponse, | |
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(dummyResponse); | |
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