Stub a Post Request for Successful Form Submission with Cypress

InstructorAndy Van Slaars

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
~ 2 years ago

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

Andy Van Slaars
Andy Van Slaars(instructor)
~ 2 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
~ 2 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
~ a year 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
~ a year ago

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