Atom 编写 spec

2018-08-12 21:50 更新

编写 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开始的描述通常会解释一个行为;而以方法名称开头的描述更像一个单元测试。

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

或者

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

添加一个或多个it方法

it方法也有两个参数,一个描述和一个函数。尝试去让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

添加一个或多个预期

了解预期(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函数。

  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'

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

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函数。(注意:如果不用beforeEach这个例子会失败)

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

带有回调的异步函数

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

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文档)来了解更多关于异步测试的细节。

运行 spec

大多数情况你会想要通过触发window:run-package-specs来运行spec。这个命令不仅仅运行包的spec,还运行了Atom的核心spec。它会运行当前项目spec目录中的所有spec。如果你想要运行Atom的核心spec和所有默认包的spec,触发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环境,类似Travis和AppVeyor中运行spec现在非常容易。详见文章“Travis CI For Your Packages”和“AppVeyor CI For Your Packages”。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号