简体   繁体   中英

How to test waterline models in Trails.js

I wanted to test the models of my Trails.js project with mocha. I use the trailpack-waterline to load my models into the Waterline ORM.

Following the Trails Docs I created a User.test.js :

'use strict'

const assert = require('assert')

describe('User Model', () => {
  let User

  before(() => {
    assert(global.app.models.User)
    User = global.app.models.User
  })

  it('should exist', () => {
    assert(User)
  })
})

This runs without any error.

But I cannot instantiate the model in any way. Following the example of the Docs new User({...}) should create a new user object, but this code throws an error saying User is not a constructor . And neither the example of the Waterline Docs using User.create({...}) seems to work.

Printing out the User model shows it consists of only two methods: [ 'getModelName', 'getTableName' ] .

How do I instantiate a waterline Model for unit testing?

It's because global.app.models.User is your model's definition and not the waterline model. This one is under global.app.orm.User , after that you can use User.create without any problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM