简体   繁体   中英

Cypress Spying on Vue components

I'm trying to write an e2e test using Cypress on my Vue app. I want to test if request on form submit returns correct value. I know i need to use cy.spy . I also know I neet to spy on fetch method but I have no idea which object to tie it to.

From documantation:

The object that has the method to be wrapped.

Which for me would be the component I'm trying to test? I'm not sure if I'm interpreting this argument correctly. Can anyone explain?

EDIT: My guess is that I need to use methods ? But if I try to import them into my test_spec.js I get Cannot find module...

To spy over a response, you can use the route command instead of spy. https://docs.cypress.io/api/commands/route.html

it('Example Test', function () {
    cy.visit('https://stackoverflow.com/');
    cy.server(); // cy.server needs to be invoked first in order to use cy.route command
    cy.route({
        url: `**/your/request/path/**`,
        method: 'POST',
        onResponse: (xhr) => {
            console.log(xhr);
            // here you can assert on xhr.response.body values
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM