【问题标题】:TypeError: Cannot read property 'splice' of undefinedTypeError:无法读取未定义的属性“拼接”
【发布时间】:2017-12-07 21:55:41
【问题描述】:

我创建了一个夹具文件来处理用于编写测试的 JSON 数据。

在每次测试之前,我希望我的数据填充种子数据。 每次测试后,我希望我的数据为空

课程.json:

[
  {
    "id": 1,
    "title": "Ma course"
  }
]

CoursesFixture.js:

const { courseList } = require('./courses')

mockData = [
  {
    "id": 1,
    "title": "Ma course"
  }
]

module.exports = {
  up: () => {
    courseList.splice(0)
    courseList.push.apply(courseList, mockData)
  },

  down: () => {
    courseList.splice(0)
  }
}

CoursesTest.js:

const request = require("supertest")
require('chai').should()
const bodyParser = require("body-parser")

const app = require('./../../app')
app.use(bodyParser.json())

const listeDeCourses = require("../fixtures/courses")
const listeDeCoursesFixture = require("../fixtures/coursesFixture")

describe('Courses', () =>{

    beforeEach(() => { listeDeCoursesFixture.up() })
    afterEach(() => { listeDeCoursesFixture.down() })

    describe('Delete course list', ()=>{
        it("Should delete a list of course", ()=>{
           return  request(app).get('/course')
                        .then((res) => {
                           res.body.should.have.lengthOf(1)
                           request(app).delete('/course').send({"id":"1"})
                           .then((res) => {
                                res.body.should.have.lengthOf(0)
                           })

                        }).catch((err)  =>{
                            throw new Error(err);
                         })
                    })
                })
    describe('Create course list', () =>{
        it("Should create a list of courses", () =>{
            return request(app).post('/course').send({"id":3,"title":"Première course"}).then((res) => {
                res.status.should.be.eq(200)
                const listCourses = res.body 
                const lastCourse = res.body[1]
                listCourses.should.be.a('array')
                lastCourse.id.should.be.eq(3)
                lastCourse.title.should.be.eq("Première course")
                listCourses[listCourses.length - 1].should.be.eq(lastCourse)
            }).catch((err) => {
                throw new Error(err)
            })
        })
    })

    describe('Get course list', ()=>{
        it("Should get a list of all courses", ()=>{
           return  request(app).get('/course')
                        .then((res) => {
                           res.body.should.have.lengthOf(1)
                        }).catch((err)  =>{
                           console.log(err)
                            throw new Error(err);
                         })
        })
    })
})

我的问题是当我启动测试时出现错误:

TypeError: Cannot read property 'splice' of undefined

我认为问题出在 CoursesFixture.js 中,并且肯定是某个地方的语法错误,但我找不到它在哪里。

【问题讨论】:

    标签: json node.js express mocha.js body-parser


    【解决方案1】:

    const { courseList } = require('./courses') 应该是const courseList = require('./courses')

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      相关资源
      最近更新 更多