2013-08-18 7 views

답변

-1

당신은 갈 : 당신이 describe에 콜백 내에서 직접하는 경우

console.log(this.title); 
+4

이 작동하지 않습니다. –

+0

이 작동하지 않습니다. @louis에서 this.test.title이어야합니다. –

23

, 당신은 describe (조상의 제목의 계층 타이틀을 얻기 위해 describe 또는 this.fullTitle()의 제목 this.title을 사용할 수 있습니다 + 이 제목). it으로 콜백 할 경우 this.test.title 또는 this.test.fullTitle()을 각각 사용할 수 있습니다. 그래서 : 출력됩니다 위

describe("top", function() { 
    console.log(this.title); 
    console.log(this.fullTitle()); 

    it("test", function() { 
     console.log(this.test.title); 
     console.log(this.test.fullTitle()); 
    }); 
}); 

console.log 문 :

function dump() { 
    console.log("running: (fullTitle)", this.test.fullTitle(), "(title)", 
       this.test.title); 
} 

function directDump() { 
    console.log("running (direct): (fullTitle)", this.fullTitle(), "(title)", 
       this.title); 
} 

describe("top", function() { 
    directDump.call(this); 
    it("test 1", dump); 
    it("test 2", dump); 
    describe("level 1", function() { 
     directDump.call(this); 
     it("test 1", dump); 
     it("test 2", dump); 
    }); 
}); 

console.log 문 출력 :

top 
top 
test 
top test 

여기에 제목이 중첩에 따라 변경하는 방법을 보여줍니다 풀러 예는 다음과 같습니다

running (direct): (fullTitle) top (title) top 
running (direct): (fullTitle) top level 1 (title) level 1 
running: (fullTitle) top test 1 (title) test 1 
running: (fullTitle) top test 2 (title) test 2 
running: (fullTitle) top level 1 test 1 (title) test 1 
running: (fullTitle) top level 1 test 2 (title) test 2 
+1

mocha의 설명서에서 'this.test.fullTitle()'과 같은 API를 문서화 할 수 있습니까? 대중이이 같은 질문에 대한 답을 알고 탐구하고 찾아내는 것이 좋을 것입니다. – Yiling

+0

아쉽게도이 영역의 설명서가 부족합니다. 이 API의 안정성에 대해 염려하는 경우 기자가이 보고서를 사용하여 보고서를 작성하므로 모카 개발자가 방금 변경 한 경우 제 3 자 기자가 중단됩니다. – Louis

+2

조금 늦었지만 https://github.com/mochajs/mocha/blob/master/lib/test.js 도움이 될 수 있습니다. –

1

beforeEach에서 this.currentTest.title을 시도하십시오.

예 : 모카 3.4.1를 사용

beforeEach(function(){ 
    console.log(this.currentTest.title); 
}) 

. 어떤 시험 방법

it('test method name'), function() { var testName= this.test.title; } 

하고 사용할 수 있습니다 내부

-1

는 :

afterEach(function(){ 
    console.log(this.currentTest.title); //displays test title for each test method  
}); 
관련 문제