2017-12-07 2 views
-1

テストを書くために使用するJSONデータを処理するフィクスチャファイルを作成しました。TypeError:未定義のプロパティ 'splice'を読み取ることができません

各テストの前に、データをシードデータで埋めたいと思っています。 各テストの後、私は私のデータが

Courses.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にあり、間違いなくどこかの構文エラーだと思いますが、どこにあるのかわかりません。

答えて

1

const { courseList } = require('./courses')const courseList = require('./courses')である必要がありますか?

+0

あなたは正しいです...私はとても愚かなhahaを感じる –

関連する問題