我們已經(jīng)通過(guò)一些例子查看并編寫(xiě)了一些spec,現(xiàn)在是更進(jìn)一步查看spec框架本身的時(shí)候了。確切地說(shuō),你在Atom中如何編寫(xiě)測(cè)試呢?
Atom使用Jasmine作為spec框架。任何新的功能都要擁有specs來(lái)防止回歸。
Atom的spec和包的spec都要添加到它們各自的spec
目錄中。下面的例子為Atom核心創(chuàng)建了一個(gè)spec。
spec文件必須以-spec
結(jié)尾,所以把sample-spec.coffee
添加到atom/spec
中。
describe
方法describe
方法有兩個(gè)參數(shù),一個(gè)描述和一個(gè)函數(shù)。以when
開(kāi)始的描述通常會(huì)解釋一個(gè)行為;而以方法名稱(chēng)開(kāi)頭的描述更像一個(gè)單元測(cè)試。
describe "when a test is written", ->
# contents
或者
describe "Editor::moveUp", ->
# contents
it
方法it
方法也有兩個(gè)參數(shù),一個(gè)描述和一個(gè)函數(shù)。嘗試去讓it
方法長(zhǎng)于描述。例如,this should work
的描述并不如it this should work
便于閱讀。但是should work
的描述要好于it should work
。
describe "when a test is written", ->
it "has some expectations that should pass", ->
# Expectations
了解預(yù)期(expectation)的最好方法是閱讀Jasmine的文檔。下面是個(gè)簡(jiǎn)單的例子。
describe "when a test is written", ->
it "has some expectations that should pass", ->
expect("apples").toEqual("apples")
expect("oranges").not.toEqual("apples")
編寫(xiě)異步的spec剛開(kāi)始會(huì)需要些技巧。下面是一些例子。
在Atom中處理Promise更加簡(jiǎn)單。你可以使用我們的waitsForPromise
函數(shù)。
describe "when we open a file", ->
it "should be opened in an editor", ->
waitsForPromise ->
atom.workspace.open('c.coffee').then (editor) ->
expect(editor.getPath()).toContain 'c.coffee'
這個(gè)方法可以在describe
、it
、beforeEach
和afterEach
中使用。
describe "when we open a file", ->
beforeEach ->
waitsForPromise ->
atom.workspace.open 'c.coffee'
it "should be opened in an editor", ->
expect(atom.workspace.getActiveTextEditor().getPath()).toContain 'c.coffee'
如果你需要等待多個(gè)promise,對(duì)每個(gè)promise使用一個(gè)新的waitsForPromise
函數(shù)。(注意:如果不用beforeEach
這個(gè)例子會(huì)失?。?/p>
describe "waiting for the packages to load", ->
beforeEach ->
waitsForPromise ->
atom.workspace.open('sample.js')
waitsForPromise ->
atom.packages.activatePackage('tabs')
waitsForPromise ->
atom.packages.activatePackage('tree-view')
it 'should have waited long enough', ->
expect(atom.packages.isPackageActive('tabs')).toBe true
expect(atom.packages.isPackageActive('tree-view')).toBe true
異步函數(shù)的Spec可以waitsFor
和runs
函數(shù)來(lái)完成。例如:
describe "fs.readdir(path, cb)", ->
it "is async", ->
spy = jasmine.createSpy('fs.readdirSpy')
fs.readdir('/tmp/example', spy)
waitsFor ->
spy.callCount > 0
runs ->
exp = [null, ['example.coffee']]
expect(spy.mostRecentCall.args).toEqual exp
expect(spy).toHaveBeenCalledWith(null, ['example.coffee'])
訪問(wèn)Jasmine文檔)來(lái)了解更多關(guān)于異步測(cè)試的細(xì)節(jié)。
大多數(shù)情況你會(huì)想要通過(guò)觸發(fā)window:run-package-specs
來(lái)運(yùn)行spec。這個(gè)命令不僅僅運(yùn)行包的spec,還運(yùn)行了Atom的核心spec。它會(huì)運(yùn)行當(dāng)前項(xiàng)目spec目錄中的所有spec。如果你想要運(yùn)行Atom的核心spec和所有默認(rèn)包的spec,觸發(fā)window:run-all-specs
命令。
要想運(yùn)行spec的一個(gè)有限的子集,使用fdescribe
和fit
方法。你可以使用它們來(lái)聚焦于單個(gè)或者幾個(gè)spec。在上面的例子中,像這樣聚焦于一個(gè)獨(dú)立的spec:
describe "when a test is written", ->
fit "has some expectations that should pass", ->
expect("apples").toEqual("apples")
expect("oranges").not.toEqual("apples")
在CI環(huán)境,類(lèi)似Travis和AppVeyor中運(yùn)行spec現(xiàn)在非常容易。詳見(jiàn)文章“Travis CI For Your Packages”和“AppVeyor CI For Your Packages”。
更多建議: