It's a bit unclear to me why the stubbing isn't working (assuming you're referring to the server response getting through).
However, the pattern for stubbed responses has become more complex now, which might cause confusion for many users.
According to my understanding of the documentation,
In cy.route(method, url, response)
, the response is referred to as the body
You need to provide a response body in order to stub the matching route.
In
cy.intercept(method, url, routeHandler?)
, the routeHandler is a more intricate concept.
routeHandler (string | object | Function | StaticResponse)
Bothobject and StaticResponse are objects - Cypress differentiates between them by examining the keys within the object, as per this explanation.
If an object with no StaticResponse keys is passed, it will be treated as a JSON response body.
The keys in a StaticResponse include:
{
fixture?: string
body?: string | object | object[]
headers?: { [key: string]: string }
statusCode?: number
forceNetworkError?: boolean
delayMs?: number
throttleKbps?: number
}
If you are including a statusCode, your object is considered a StaticResponse, and hence the message and result should be moved into the body section,
cy.intercept('GET',
`${API}farm/list`,
{
statusCode: 200,
body: {
message: 'Request successful',
result: seededFarmList
}
}
);
In my opinion, they may have over-complicated things slightly - the transition from StaticResponse to object based on keys seems somewhat unnecessary.
I came across an example in the sample spec file network_requests.spec.js
(included in the initial run of Cypress).
beforeEach(() => {
cy.visit('https://example.cypress.io/commands/network-requests')
})
...
let message = 'whoa, this comment does not exist'
// Stub a response to PUT comments/ ****
cy.intercept({
method: 'PUT',
url: '**/comments/*',
}, {
statusCode: 404,
body: { error: message }, // stub returns above message
headers: { 'access-control-allow-origin': '*' },
delayMs: 500,
}).as('putComment')
// we have code that puts a comment when
// the button is clicked in scripts.js
cy.get('.network-put').click()
cy.wait('@putComment')
// our 404 statusCode logic in scripts.js executed
cy.get('.network-put-comment').should('contain', message)