2017-12-07 10 views
-1

테스트 작성에 사용되는 JSON 데이터를 처리하기위한 조명기 파일을 생성했습니다.TypeError : 정의되지 않은 'splice'속성을 읽을 수 없습니다.

각 테스트를 수행하기 전에 데이터에 시드 데이터를 입력해야합니다.

[ 
    { 
    "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 : 각 테스트 후, 나는 내 데이터가

Courses.json 비어 있어야 할

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

당신 말이 맞아요 ... 너무 바보 같아요. 하하. –