处理CRUD测试的最佳方法?

时间:2019-03-15 08:23:56

标签: hapijs hapi lab

我想为我的CRUD端点编写一个测试。但是我要测试“ UPDATE”,“ DELETE”端点是否依赖于“ CREATE”端点中创建的文档。

例如:

let createdAccount = null;
it("should create an account", async () => {
  const response = await Server.inject({
    method: "POST",
    url: "/v1/accounts",
    payload: JSON.stringify({
      name: "TEST",
      email: "test@test.com"
    })
  });
  expect(response.statusCode).to.equal(200);
  expect(response.result).to.a.object();
  expect(response.result._id).to.exists();
  createdAccount = response.result;
});

it("should delete an account", async () => {
  const deleteResponse = await Server.inject({
    method: "DELETE",
    url: `/v1/accounts/${createdAccount._id}`
  });
  expect(deleteResponse.statusCode).to.equal(200);
  expect(deleteResponse.result).to.a.object();
  expect(deleteResponse.result._id).to.exists();
});

处理此问题的最佳方法是什么?我应该创建一个依赖于另一个人的结果的测试用例吗?

注意:我正在使用 hapijs ,hapi / 实验室,hapi / 代码进行测试。

1 个答案:

答案 0 :(得分:1)

您的用例完全可以。我们在测试案例中也使用了类似的方法。

这是来自真实应用程序的一段测试代码。

describe('Validate campaign routes', () => {

    let server, token, cookie, campaignId;
    before(async () => {
        server = await Glue.compose(serverConfig.manifest, options);

        // custom my api related stuff, such as JTW token generation user creation etc.
    });

    after(async () => {
        await mongoose.connection.db.dropDatabase();
        await server.stop();
        await helpers.delay(100);
    });

    it("should create a campaign", async () => {
        const res = await server.inject({
            url: '/campaign',
            method: 'post',
            payload: {
                name: "Sample Campaign",
                active: true
            },
            headers: {
                "Authorization": token,                
            }
        });


        expect(res.statusCode).to.equal(200);
        expect(res.result.created).to.exist();
        expect(res.result.created.name).to.equal("Sample Campaign");
        expect(res.result.status).to.equal(true);
        campaignId = res.result.created.id;
    });

    it("should fetch all campaigns", async () => {
        const res = await server.inject({
            url: '/campaign?page=1',
            method: 'get',
            headers: {
                "Authorization": token,
            }
        });

        expect(res.statusCode).to.equal(200);
        expect(res.result.status).to.equal(true);
        expect(res.result.results).to.be.an.array();
        expect(res.result.results).to.have.length(1);
        expect(res.result.results[0].name).to.equal('Sample Campaign');
    });


    it("should fetch campaign details", async () => {
        // fetch campaign details
        const res2 = await server.inject({
            url: `/campaign/${campaignId}`,
            method: 'get',
            headers: {
                "Authorization": token,
            }
        });

        expect(res2.statusCode).to.equal(200);
        expect(res2.result).to.be.an.object();
        expect(res2.result.name).to.equal('Sample Campaign');
    });            

    it("should update campaign", async () => {            
        const res = await server.inject({
            url: `/campaign/${campaignId}`,
            method: 'put',
            payload: {
                name: "Updated Campaign Title",
                maxImpression: 1000,
                endDate: "01-04-2019"
            },
            headers: {
                "Authorization": token,
            }
        });

        expect(res.statusCode).to.equal(200);
        expect(res.result).to.be.an.object();
        expect(res.result.updated.name).to.equal('Updated Campaign Title');
        expect(res.result.updated.maxImpression).to.equal(1000);
    });


    it("should delete campaign", async () => {           
        const res = await server.inject({
            url: `/campaign/${campaignId}`,
            method: 'delete',
            headers: {
                "Authorization": token,
            }
        });

        expect(res.statusCode).to.equal(200);
        expect(res.result.deleted).to.equal(campaignId);


        const res2 = await server.inject({
            url: '/campaign',
            method: 'get',
            headers: {
                "Authorization": token,
            }
        });

        expect(res2.statusCode).to.equal(200);
        expect(res2.result.status).to.equal(true);
        expect(res2.result.results).to.be.an.array();
        expect(res2.result.results).to.have.length(0);
    });
});