编写 spec

      我们已经通过一些例子查看并编写了一些spec,现在是更进一步查看spec框架本身的时候了。确切地说,你在Atom中如何编写测试呢?

      Atom使用Jasmine作为spec框架。任何新的功能都要拥有specs来防止回归。

      创建新的 spec

      Atom的spec包的spec都要添加到它们各自的spec目录中。下面的例子为Atom核心创建了一个spec。

      创建spec文件

      spec文件必须以-spec结尾,所以把sample-spec.coffee添加到atom/spec中。

      添加一个或多个describe方法

      describe方法有两个参数,一个描述和一个函数。以when开始的描述通常会解释一个行为;而以方法名称开头的描述更像一个单元测试。

      1. describe “when a test is written”, ->
      2. # contents

      或者

      1. describe “Editor::moveUp”, ->
      2. # contents

      添加一个或多个it方法

      it方法也有两个参数,一个描述和一个函数。尝试去让it方法长于描述。例如,this should work的描述并不如it this should work便于阅读。但是should work的描述要好于it should work

      1. describe “when a test is written”, ->
      2. it “has some expectations that should pass”, ->
      3. # Expectations

      添加一个或多个预期

      了解预期(expectation)的最好方法是阅读Jasmine的文档。下面是个简单的例子。

      1. describe “when a test is written”, ->
      2. it “has some expectations that should pass”, ->
      3. expect(“apples”).toEqual(“apples”)
      4. expect(“oranges”).not.toEqual(“apples”)

      异步的spec

      编写异步的spec刚开始会需要些技巧。下面是一些例子。

      Promise

      在Atom中处理Promise更加简单。你可以使用我们的waitsForPromise函数。

      1. describe “when we open a file”, ->
      2. it “should be opened in an editor”, ->
      3. waitsForPromise ->
      4. atom.workspace.open(‘c.coffee’).then (editor) ->
      5. expect(editor.getPath()).toContain ‘c.coffee’

      这个方法可以在describeitbeforeEachafterEach中使用。

      1. describe “when we open a file”, ->
      2. beforeEach ->
      3. waitsForPromise ->
      4. atom.workspace.open ‘c.coffee’
      5. it “should be opened in an editor”, ->
      6. expect(atom.workspace.getActiveTextEditor().getPath()).toContain ‘c.coffee’

      如果你需要等待多个promise,对每个promise使用一个新的waitsForPromise函数。(注意:如果不用beforeEach这个例子会失败)

      1. describe “waiting for the packages to load”, ->
      2. beforeEach ->
      3. waitsForPromise ->
      4. atom.workspace.open(‘sample.js’)
      5. waitsForPromise ->
      6. atom.packages.activatePackage(‘tabs’)
      7. waitsForPromise ->
      8. atom.packages.activatePackage(‘tree-view’)
      9. it ‘should have waited long enough’, ->
      10. expect(atom.packages.isPackageActive(‘tabs’)).toBe true
      11. expect(atom.packages.isPackageActive(‘tree-view’)).toBe true

      带有回调的异步函数

      异步函数的Spec可以waitsForruns函数来完成。例如:

      1. describe “fs.readdir(path, cb)”, ->
      2. it “is async”, ->
      3. spy = jasmine.createSpy(‘fs.readdirSpy’)
      4. fs.readdir(‘/tmp/example’, spy)
      5. waitsFor ->
      6. spy.callCount > 0
      7. runs ->
      8. exp = [null, [‘example.coffee’]]
      9. expect(spy.mostRecentCall.args).toEqual exp
      10. expect(spy).toHaveBeenCalledWith(null, [‘example.coffee’])

      访问Jasmine文档)来了解更多关于异步测试的细节。

      运行 spec

      大多数情况你会想要通过触发window:run-package-specs来运行spec。这个命令不仅仅运行包的spec,还运行了Atom的核心spec。它会运行当前项目spec目录中的所有spec。如果你想要运行Atom的核心spec和所有默认包的spec,触发window:run-all-specs命令。

      要想运行spec的一个有限的子集,使用fdescribefit方法。你可以使用它们来聚焦于单个或者几个spec。在上面的例子中,像这样聚焦于一个独立的spec:

      1. describe “when a test is written”, ->
      2. fit “has some expectations that should pass”, ->
      3. expect(“apples”).toEqual(“apples”)
      4. expect(“oranges”).not.toEqual(“apples”)

      在CI中运行

      在CI环境,类似Travis和AppVeyor中运行spec现在非常容易。详见文章“Travis CI For Your Packages”和“AppVeyor CI For Your Packages”。