Discovering the Power of Pact in Conjunction with Angular.
Encountering an unexpected error during test execution. Error: The actual interactions do not align with the expected interactions for mock MockService.
Requests that are Missing:
GET /dogs
Source code being tested: dog.service.ts
@Injectable({
providedIn: 'root'
})
export class DogService {
constructor(private http: HttpClient) {
}
getDogs(baseUrl: string): Observable<Dog[]>{
return this.http.get<Dog[]>(baseUrl + '/dogs');
}
}
Spec file:
import {TestBed} from '@angular/core/testing';
import {HttpClientModule} from '@angular/common/http';
import {PactWeb, Matchers} from '@pact-foundation/pact-web';
import { DogService } from './dog.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import {Pact} from '@pact-foundation/pact';
describe('Contract Tests for Dog Service', () => {
let provider: PactWeb;
let dogService: DogService;
beforeAll(async (done) => {
provider = new PactWeb({
port: 1234,
host: '127.0.0.1',
log: '.\\log\\pact.log',
logLevel: 'warn',
dir: '.\\pacts',
spec: 2
});
// Required for slower CI environments
setTimeout(done, 2000);
// Necessary if run with `singleRun: false`
await provider.removeInteractions();
console.log('Creating Pact web instance');
});
afterEach(async () => {
await provider.verify();
console.log('Verifying Interactions');
});
afterAll(async () => {
return await provider.finalize();
console.log('Finalizing Pact');
});
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
DogService
]
});
console.log('Configuring Test Module');
});
describe('Standards Testing', () => {
beforeAll(async () => {
// setting up Pact interactions
await provider.addInteraction({
state: 'dogs exist',
uponReceiving: 'fetch all dogs',
withRequest: {
method: 'GET',
path: '/dogs'
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: Matchers.eachLike({
avatar: Matchers.like('xyz'),
title: Matchers.like('German Shepard'),
subTitle: Matchers.like('This is German Shepard'),
imageUrl: Matchers.like('xyz'),
description: 'Lorem ipsum'
}, {min: 2}),
},
});
console.log('Adding Interaction');
});
it('Validating Dog Details Existence', async () => {
dogService = TestBed.inject(DogService);
console.log(provider.mockService.baseUrl);
dogService.getDogs(provider.mockService.baseUrl).subscribe((response) => {
expect(response).toBeDefined();
console.log(response);
});
});
});
});
Could there be a specific issue causing this discrepancy?
Package.json: "@pact-foundation/karma-pact": "^2.3.1", "@pact-foundation/pact": "^9.11.0", "@pact-foundation/pact-node": "^10.9.4", "@pact-foundation/pact-web": "^9.11.0",