Atom 編寫 spec

2018-08-12 21:50 更新

編寫 spec

我們已經(jīng)通過一些例子查看并編寫了一些spec,現(xiàn)在是更進(jìn)一步查看spec框架本身的時候了。確切地說,你在Atom中如何編寫測試呢?

Atom使用Jasmine作為spec框架。任何新的功能都要擁有specs來防止回歸。

創(chuàng)建新的 spec

Atom的spec包的spec都要添加到它們各自的spec目錄中。下面的例子為Atom核心創(chuàng)建了一個spec。

創(chuàng)建spec文件

spec文件必須以-spec結(jié)尾,所以把sample-spec.coffee添加到atom/spec中。

添加一個或多個describe方法

describe方法有兩個參數(shù),一個描述和一個函數(shù)。以when開始的描述通常會解釋一個行為;而以方法名稱開頭的描述更像一個單元測試。

describe "when a test is written", ->
  # contents

或者

describe "Editor::moveUp", ->
  # contents

添加一個或多個it方法

it方法也有兩個參數(shù),一個描述和一個函數(shù)。嘗試去讓it方法長于描述。例如,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ù)期

了解預(yù)期(expectation)的最好方法是閱讀Jasmine的文檔。下面是個簡單的例子。

describe "when a test is written", ->
  it "has some expectations that should pass", ->
    expect("apples").toEqual("apples")
    expect("oranges").not.toEqual("apples")

異步的spec

編寫異步的spec剛開始會需要些技巧。下面是一些例子。

Promise

在Atom中處理Promise更加簡單。你可以使用我們的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'

這個方法可以在describe、it、beforeEachafterEach中使用。

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'

如果你需要等待多個promise,對每個promise使用一個新的waitsForPromise函數(shù)。(注意:如果不用beforeEach這個例子會失?。?/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

帶有回調(diào)的異步函數(shù)

異步函數(shù)的Spec可以waitsForruns函數(shù)來完成。例如:

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'])

訪問Jasmine文檔)來了解更多關(guān)于異步測試的細(xì)節(jié)。

運行 spec

大多數(shù)情況你會想要通過觸發(fā)window:run-package-specs來運行spec。這個命令不僅僅運行包的spec,還運行了Atom的核心spec。它會運行當(dāng)前項目spec目錄中的所有spec。如果你想要運行Atom的核心spec和所有默認(rèn)包的spec,觸發(fā)window:run-all-specs命令。

要想運行spec的一個有限的子集,使用fdescribefit方法。你可以使用它們來聚焦于單個或者幾個spec。在上面的例子中,像這樣聚焦于一個獨立的spec:

describe "when a test is written", ->
  fit "has some expectations that should pass", ->
    expect("apples").toEqual("apples")
    expect("oranges").not.toEqual("apples")

在CI中運行

在CI環(huán)境,類似Travis和AppVeyor中運行spec現(xiàn)在非常容易。詳見文章“Travis CI For Your Packages”和“AppVeyor CI For Your Packages”。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號