Untitled
unknown
plain_text
a year ago
2.2 kB
8
Indexable
teste da controller req 3 it('should return 400 if "name" is missing', async function () { const req = { body: {} }; const res = { status: sinon.stub().returnsThis(), json: sinon.stub().returnsThis(), }; await productsController.insertNewProduct(req, res); expect(res.status).to.have.been.calledWith(400); expect(res.json).to.have.been.calledWith({ message: '"name" is required' }); }); it('should return 422 if "name" length is less than 5', async function () { const req = { body: { name: 'abc' } }; const res = { status: sinon.stub().returnsThis(), json: sinon.stub().returnsThis(), }; await productsController.insertNewProduct(req, res); expect(res.status).to.have.been.calledWith(422); expect(res.json).to.have.been.calledWith({ message: '"name" length must be at least 5 characters long' }); }); it('should call productsService.insertNewProduct and return 201 with the new product', async function () { const req = { body: { name: 'Product Name' } }; const res = { status: sinon.stub().returnsThis(), json: sinon.stub().returnsThis(), }; const mockNewProduct = { id: 1, name: 'Product Name' }; sinon.stub(productsService, 'insertNewProduct').resolves(mockNewProduct); await productsController.insertNewProduct(req, res); expect(productsService.insertNewProduct).to.have.been.calledWith('Product Name'); expect(res.status).to.have.been.calledWith(201); expect(res.json).to.have.been.calledWith(mockNewProduct); }); it('should return 404 if an error occurs', async function () { const req = { body: { name: 'Product Name' } }; const res = { status: sinon.stub().returnsThis(), json: sinon.stub().returnsThis(), }; const errorMessage = 'Error message'; sinon.stub(productsService, 'insertNewProduct').throws(new Error(errorMessage)); await productsController.insertNewProduct(req, res); expect(productsService.insertNewProduct).to.have.been.calledWith('Product Name'); expect(res.status).to.have.been.calledWith(404); expect(res.json).to.have.been.calledWith({ message: errorMessage }); });
Editor is loading...
Leave a Comment