2013-08-18 18 views

cevap

-1

gittiğiniz: Eğer describe için bir geri içine doğrudan değilseniz

console.log(this.title); 
+4

Bu işe yaramaz. –

+0

çalışmıyor. –

23

, sen describe (atalarının başlıkların hiyerarşik unvanını almak için describe veya this.fullTitle() unvanı için this.title kullanabilirsiniz + bunun başlığı). it numaralı telefona geri dönüyorsanız, sırasıyla this.test.title veya this.test.fullTitle() kullanabilirsiniz. Yani: çıktısı şöyle olur

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 ifadeleri:

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 ifadeleri olacak çıkışı:

top 
top 
test 
top test 

İşte başlıklar yuvalama bağlı olarak değişir nasıl gösteren bir dolgun örnek:

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

'un altında @louis yanıtında olduğu gibi this.test.title olması gerekir. "Bu.test.fullTitle()" gibi bir API belgelenirse, Mocha'nın belgelerine işaret edebilir misiniz? Halkın bunu ve benzeri soruları tanıması, araştırması ve cevaplarını bulması harika olurdu. – Yiling

+0

Maalesef bu alandaki belgeler eksik. Bu API'nın kararlılığından endişe ediyorsanız, söyleyebildiğim şey, gazetecilerin raporlarını üretmek için bunu kullanmalarıdır, bu yüzden Mocha devreleri yeni değiştiyse üçüncü taraf muhabirlerini bozarlar. – Louis

+2

Biraz geç, ama bu yardımcı olabilir: https://github.com/mochajs/mocha/blob/master/lib/test.js –

1

beforeEach'dan, this.currentTest.title'u deneyin.

Örnek: Mocha 3.4.1 kullanarak

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

. herhangi bir test yöntemine

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

ve kullanmak olabilir İçinde

-1

:

afterEach(function(){ 
    console.log(this.currentTest.title); //displays test title for each test method  
}); 
İlgili konular