如何使用cypress.io更改db中记录的属性 [英] How to change property of a record in db with cypress.io

查看:203
本文介绍了如何使用cypress.io更改db中记录的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是赛普拉斯的新手,我想知道如何进行以下检查:
我有这种情况:
我在数据库中有一个产品,其状态可以是:InStock和OutOfStock并停产。如果产品处于 InStock状态,则应该可以将其发送给客户;如果产品处于 OutOfStock /已停产状态,则不能将其发送给客户。
通过api调用,我可以将产品分发给客户。如果产品处于 InStock状态,则api响应为200,否则响应为statusCode400。
我的问题是:如何为每个测试更改数据库中产品的状态,因此我可以检查3种状态中的每一种(如果api返回正确的响应)?我知道如何检查api响应本身,但是我不清楚如何在每次测试之前更改数据库中产品的状态。

i'm new to cypress and i'm wondering how can i do the following checks: I have case: I have a product in the db, that can have statuses: InStock and OutOfStock and Discontinued. If product is in 'InStock ' status, i should be able to dispatch it to a customer, if in 'OutOfStock'/ 'Discontinued' i should not be able to dispatch to a customer. With an api call i can dispatch the product to a customer. If product is in 'InStock' status, the api response is 200, if not the response is with statusCode 400. So my question is: How can i change the status of the product in the db for each test, so i can check each of the 3 statuses (if the api returns the proper response)? I know how to check the api response itself, but it's not clear to me how to change the status of the product in the db, before each test.

推荐答案

与@Maccurt不同,我实际上按照您提出的方式(更改数据库)进行操作,因此您可以正确地测试e2e流量。

Unlike @Maccurt, I would actually do it the way you propose (change the DB), so you can properly test e2e flow.

您可以使用 cy.task 与Cypress Runner的节点进程进行对话,该进程可以直接写入数据库。看起来像这样:

And instead of setting up test routes which would write to the DB you can use cy.task to talk to the Cypress Runner's node process which can write to the DB directly. It'd look like this:

使用postgres( node-postgres )。

Using postgres (node-postgres) in this example.

your-test.spec.js

describe("test", () => {
    it("test", () => {
        cy.task("query", {
            sql: `
                UPDATE product
                SET status = $1
                WHERE id = $2
            `,
            values: [ "InStock", 40 ]
        });

        // your cy assertions here
    });
});

cypress / plugins / index.js

const pg = require("pg");
const pool = /* initialize your database connection */;

module.exports = ( on ) => {

    on( "task", {
        query ({ sql, values }) {

            return pool.query( sql, values );
        }
    });
});

这篇关于如何使用cypress.io更改db中记录的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆