Last active
January 8, 2019 15:20
-
-
Save rorymcgit/0bb82714b2de91fde8769b4acd668c2f 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 dummyResponseForAssertion = { | |
foo: [{ | |
bar: ['a string'], | |
}], | |
}; | |
const dummyResponseForPact = Matchers.somethingLike({ | |
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: dummyResponseForPact, | |
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(dummyResponseForAssertion); | |
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