Sqlite3表不存在,React

时间:2018-08-01 07:46:56

标签: database reactjs testing server sqlite

我正在尝试为我的数据库功能编写测试。第一个测试正在运行,但是我收到了下面为“ AddExercise”测试编写的错误。请在下面查看我的代码。

这是种子文件:

exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('exercises').del()
.then(function () {
  // Inserts seed entries
  return knex('exercises').insert([
    {id: 1, exe_name: 'rowing', exe_info:'For back and chest', 
  comment:'Good', 
  exe_url:'https://www.freetrainers.com/exercise/calendar/'}        
  ]);});};

这些是数据库功能:

 const db = require('./connection')

 const getExercises = (testDb) => {
 return (testDb || db)('exercises')
.select()}

 const getExerciseById = (id, testDb) => {
 return (testDb || db)('exercises')
.select()
.where('exercises.id', id)}

const addExercise = (exercise, testDb) => {
return (testDb || db)('exercises').insert(exercise)
.then(exercise_id => getExerciseById(exercise_id[0], db))}

这是测试文件:

const request = require('supertest')
const env = require('./test-environment')
const exercises = require('../../server/db/exercises')

let testDb = null
beforeEach(() => {
testDb = env.getTestDb()
return env.initialise(testDb) })
afterEach(() => env.cleanup(testDb))

//测试

test('getAllexercises function gets all exercises', () => {
const expected = 1
return exercises.getExercises(testDb)
.then(exercises => {
  const actual = exercises.length
  expect(actual).toBe(expected)
})})

// AddExercise

test('addExercise functions adds an exercise', () => {
const fakeExercise = {
exe_name: 'Test',
exe_info: 'Test title',
comment: 'dsfasdfdf',
exe_url: 'fake/blah'    
}
const expected = {
...fakeExercise,
id:2}
return exercises.addExercise(fakeExercise, testDb)
.then(actual => {
  expect(actual).toBe(expected)
  return testDb('exercises')
})
.then(exercises => {
  expect(exercises.length).toBe(expected)
})})

运行纱线测试db.test.js时,我得到以下信息:

Error: SQLITE_ERROR: no such table: exercises
Ran all test suites matching /db.test.js/i.Jest did not exit one 
second after the test run has completed.

This usually means that there are asynchronous operations that weren't 
stopped in your tests. 
Consider running Jest with `--detectOpenHandles` to troubleshoot this 
issue.
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c jest db.test.js
Directory: /home/eda/new/Gym-app
Output:
".
info If you think this is a bug, please open a bug report with the 
information provided in "/home/hameetsingh/eda/new/Gym-app/yarn- 
error.log"

我的package.json和knexfile配置似乎很好,因为另一个项目具有类似的设置,并且测试似乎也可以正常工作。请指教。谢谢

0 个答案:

没有答案