0
0
Microservicessystem_design~10 mins

Contract testing (Pact) in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Pact consumer test with the correct provider name.

Microservices
const pact = new Pact({ consumer: 'UserService', provider: '[1]' });
Drag options to blanks, or click blank then click option'
APaymentService
BOrderService
CUserService
DInventoryService
Attempts:
3 left
💡 Hint
Common Mistakes
Using the consumer name as the provider name.
Confusing provider and consumer roles.
2fill in blank
medium

Complete the code to specify the HTTP method in the Pact interaction.

Microservices
pact.addInteraction({ uponReceiving: 'a request for order details', withRequest: { method: '[1]', path: '/orders/123' }, willRespondWith: { status: 200 } });
Drag options to blanks, or click blank then click option'
APUT
BPOST
CGET
DDELETE
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for data retrieval.
Confusing HTTP methods for different operations.
3fill in blank
hard

Fix the error in the Pact interaction to correctly specify the expected response status code.

Microservices
pact.addInteraction({ uponReceiving: 'a request for payment status', withRequest: { method: 'GET', path: '/payments/456' }, willRespondWith: { status: [1] } });
Drag options to blanks, or click blank then click option'
A200
B500
C404
D201
Attempts:
3 left
💡 Hint
Common Mistakes
Using 201 which means resource created, not appropriate here.
Using error codes like 404 or 500 for successful responses.
4fill in blank
hard

Fill both blanks to define the expected JSON response body with correct keys and values.

Microservices
willRespondWith: { status: 200, body: { orderId: [1], status: '[2]' } }
Drag options to blanks, or click blank then click option'
A123
Bconfirmed
Cpending
D456
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string for orderId instead of a number.
Choosing a status that does not match the expected order state.
5fill in blank
hard

Fill all three blanks to create a Pact test that verifies the provider and writes the pact file.

Microservices
pact.setup().then(() => pact.verify()).then(() => pact.[1]()).then(() => pact.[2]()).catch(e => console.error(e));

// After tests
pact.[3]();
Drag options to blanks, or click blank then click option'
AwritePact
Bfinalize
Ccleanup
DpublishPact
Attempts:
3 left
💡 Hint
Common Mistakes
Calling publishPact() instead of writePact() or finalize().
Not cleaning up resources after tests.