cancel
Showing results for 
Search instead for 
Did you mean: 

cds.test using jest and axios not working for POST request

sahu101
Explorer
0 Kudos
85

I have written a unit test where I am trying to test a CREATE API which vaildates the data and increments the material number. 

this is the service defintion

 

service MaterialService {
  entity Materials as projection on db.Materials;
}

 

this is the service implementation

 

export class MaterialService extends ApplicationService {

  init() {

    this.before("POST", "Materials", async (req: Request) => this.validateMaterial(req))

    return super.init()

  }

  async validateMaterial(req: Request) {
    let material: Material = req.data

    const { Materials } = this.entities

    // Validate Material Type

    if (material.type != "ABC" && material.type != "XYZ") {
      req.error({
        code: 500,
        message: "Invalid Material Type"
      })
    }

    // generate a material number, +1 the last matnr

    let latestMaterialNumber = await SELECT.one.from(Materials).orderBy(`matnr desc`)

    if (latestMaterialNumber) {
      material.matnr = latestMaterialNumber.matnr + 1
    } else {
      material.matnr = 1
    }

    req.data = material

    return req

  }

}

 

and, this is the test

 

describe(('material-service-test'), () => {
  const { POST } = cds.test(__dirname + '/..')

  it('should create material successfully', async () => {

    const material = {
      name: "Samsung Galaxy S24",
      description: "Smartphone",
      type: "ABCD"
    }

    const response = await POST('/odata/v4/material/Materials', material)
    console.log(response)
})

 

I get the following error message, which indicates that the this.before() hook is not being called while making the POST request

 

  material-service-test
    × should create material successfully (381 ms)                                                                    
                                                                                                                      
  ● material-service-test › should create material successfully                                                       
                                                                                                                      
    SQLITE_CONSTRAINT_NOTNULL - NOT NULL constraint failed: db_Materials.matnr                                        
                                                                                                                      
      29 |     }                                                                                                      
      30 |                                                                                                            
    > 31 |     const response = await POST('/odata/v4/material/Materials', material)
         |                      ^
      32 |
      33 |     console.log(response)
      34 |

      at Object.<anonymous> (test/material-service.test.js:31:22)

    Cause:
    NOT NULL constraint failed: db_Materials.matnr

      29 |     }
      30 |
    > 31 |     const response = await POST('/odata/v4/material/Materials', material)
         |                      ^
      32 |
      33 |     console.log(response)
      34 |

      at _error (node_modules/@cap-js/cds-test/lib/axios.js:59:55)
      at Object.<anonymous> (test/material-service.test.js:31:22)

 

How can I fix this issue?

Thanks 

Accepted Solutions (0)

Answers (0)