在nodejs app上使用sqlite3测试失败

时间:2017-09-28 10:50:55

标签: node.js sqlite

我尝试测试数据库查询,并且我使用Objection这样的ORM和knex

项目应用程序的结构:

/project-sqlite
  /__tests__
    routes.test.js
  /models
    user.js
  /node_modules
  -app.js
  -package-lock.json
  -package.json
  -user.db

模型/ user.js的:

const objection = require('objection');
const Model = objection.Model;
const Knex = require('knex');

const knex = Knex({
  client: 'sqlite3',
  useNullAsDefault: true,
  connection: {
    filename: './user.db'
  }
});

Model.knex(knex);

// Create database schema. You should use knex migration files to do this. We
// create it here for simplicity.
const schemaPromise = knex.schema.createTableIfNotExists('user', table => {
  table.increments('id').primary();
  table.string('name');
});

class User extends Model {
  static get tableName() {
    return 'user';
  }

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['name'],

      properties: {
        id: { type: 'integer' },
        name: { type: 'string', minLength: 1, maxLength: 255 }
      }
    };
  }
};

schemaPromise.then(() => {
  return User.query().insert({name: 'User 1'});
}).then(user => {
  console.log('created: ', user.name, 'id: ', user.id);
});

module.exports = User;

app.js:

const express = require('express');
const User = require('./models/user');

app = express();

app.get('/users', (req, res) => {
  const newUser = {
    name: 'user 4050'
  };
  User
  .query()
  .insert(newUser)
  .then(user => {
    res.writeHead(200, {'Content-type': 'text/json'});
    res.end(JSON.stringify(user));
  })

})

app.get('/users1', (req, res) => {
  User
  .query()
  .then(user => {
    res.writeHead(200, {'Content-type': 'text/json'});
    res.end(JSON.stringify(user));
  })

})

app.listen(3000, ()=> {
  console.log('server runs on localhost:3000');
});

测试 /routes.test.js:

const user = require('../models/user');
describe('Test model User', () => {
  test('should return User 1', (done) => {
    user
    .query()
    .where('id', 1)
    .then(userFounded => {
       expect(userFounded.name).toEqual('User 1');
       done();
    })
  });
});

测试失败,但我插入了一些用户,我收到了这个错误:

Test model User
✕ should return User 1 (29ms)

  ● Test model User › should return User 1

    expect(received).toEqual(expected)

    Expected value to equal:
      "User 1"
    Received:
      undefined

    Difference:

      Comparing two different types of values. Expected string but received undefined.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sqlite@1.0.0 test: `jest`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the sqlite@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我不明白这是什么问题。这是否是由于与存储数据的文件没有连接或没有?

1 个答案:

答案 0 :(得分:0)

library(diagram) data <- c(0, "'.47*'", 0, 0, 0, 0, "'.36*'", "'.33* (.16)'", 0) M<- matrix (nrow=3, ncol=3, byrow = TRUE, data=data) plot<- plotmat (M, pos=c(1,2), name= c( "Math self-efficacy","Math ability", "Interest in the math major"), box.type = "rect", box.size = 0.12, box.prop=0.5, curve=0) 的类型是一个数组而不是一个对象,因此您无法获得与userFounded一起使用的用户名。

尝试:

userFounded.name