Stub a Post Request for Successful Form Submission with Cypress

Share this video with your friends

Send Tweet

In this lesson well stub a POST request and use Cypress commands to fill in and submit a form. We’ll wait for the submission to resolve and then assert that the new item was added to the list.

Ben
Ben
~ 6 years ago

Why does cy.route include that response; why not just a status 200?

Andy Van Slaars
Andy Van Slaars(instructor)
~ 6 years ago

Because our application code uses the response to update the UI. We need to stub the response body to test the intended behavior of the application.

Ivan
Ivan
~ 5 years ago

Can't we just place cy.seedAndVisit() before cy.route(...) as it already contains cy.server()? Are there negative sides to calling cy.server() several times?

Jonathan Picazo
Jonathan Picazo
~ 5 years ago

Does creating that stub mean it won't actually send to the real database when you hit enter? So... it just knows that you created a stub beforehand and will return the post response?

Zac Jones
Zac Jones
~ 5 years ago

You're correct @Jonathan - the test will use the stubbed out data without having to send any requests or fetch from the database