【发布时间】:2020-04-02 19:47:49
【问题描述】:
我正在尝试生成一些断言来测试我的 Restful CRUD API,但我不是如何测试新生成的文档。我也在使用 Express + Firebase,所以我能够轻松地测试用户,因为 ID 就是电子邮件本身。但对于任何其他文档,ID 都是随机创建的。
例如,我的用户测试如下所示:
describe('POST - /api/admin/users', () => {
it('Should create a user', done => {
const user: IUser = {
name: 'User TDD Test',
email: 'tdd@test.com',
password: '123456',
createdAt: 1585848131576
};
chai.request(server)
.post('/api/admin/users')
.send(user)
.end((err, response) => {
response.should.have.status(201);
response.body.should.be.an.instanceOf(Object);
response.body.should.have.property('message').eq('User created');
response.body.should.have.property('data')
.and.be.an.instanceOf(Object)
.and.have.property('name').eq('UserTDD Test');
response.body.should.have.property('ok').eq(true);
done();
});
});
});
describe('PUT - /api/admin/users/:email', () => {
it('Should update a user', done => {
const email = 'tdd@test.com'
const user: object = {
password: '1234567',
updatedAt: 1585848131576
};
chai.request(server)
.put('/api/admin/users/' + email)
.send(user)
.end((err, response) => {
response.should.have.status(201);
response.body.should.be.an.instanceOf(Object);
response.body.should.have.property('message').eq('User updated');
response.body.should.have.property('data').eq(null);
response.body.should.have.property('ok').eq(true);
done();
});
});
});
在这里很容易更新同一个文档,因为它们共享同一个电子邮件。但是,例如,如果我想使用生成的 ID 更新任何其他集合,我将无法以相同的方式测试创建和更新,对吗?
例如,如果我对集合“tickets”有相同的代码:
describe('POST - /api/admin/tickets', () => {
it('Should create a ticket', done => {
const ticket: ITicket = {
owner: 'tdd@test.com',
status: 'Open',
createdAt: 1585848131576
};
chai.request(server)
.post('/api/admin/tickets')
.send(ticket)
.end((err, response) => {
response.should.have.status(201);
response.body.should.be.an.instanceOf(Object);
response.body.should.have.property('message').eq('Created ticket');
response.body.should.have.property('data')
.and.be.an.instanceOf(Object)
.and.have.property('owner').eq('tdd@test.com');
response.body.should.have.property('ok').eq(true);
done();
});
});
});
那么,之前生成的工单有一个未知值的 ID 属性,那么,我应该为另一个具有已知 ID 的工单测试“PUT 方法”吗?
【问题讨论】:
-
请注意,我们更喜欢这里的技术写作风格。我们轻轻地劝阻问候,希望你能帮助,谢谢,提前感谢,感谢信,问候,亲切的问候,签名,请你能帮助,聊天材料和缩写 txtspk,恳求,你多久了被卡住、投票建议、元评论等。只需解释您的问题,并展示您尝试过的内容、预期的内容以及实际发生的情况。
-
您将不得不编辑我所有的问题,因为我在每个问题中都添加了这些温柔的问候。没有人报告我添加它们。
-
我会尝试解决它,尽管在这里这是一个愚蠢的差事。如果您以后的问题至少可以写得更简洁,志愿编辑将不胜感激!我们几乎没有足够的人来处理传入的材料量。人们不会“报道”这类闲聊材料,但 Meta 上有很多关于对简洁和技术写作的渴望的参考。跨度>
标签: javascript node.js google-cloud-firestore mocha.js chai