How to test waterline models in Trails.js
Asked Answered
D

1

7

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?

Dm answered 8/11, 2017 at 13:3 Comment(0)
S
1

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

Stanton answered 10/1, 2018 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.