2

我正在尝试为 Atom.io 编辑器编写一个简单的包。这是我第一次使用 Coffeescript。所以我可能错过了一些微不足道的东西。

无论如何,这是我的 index.coffee

module.exports =
 activate: ->
   atom.workspaceView.command "md-utils:unorderedList", => @unorderedList()
 unorderedList: ->
   out = ""
   editor = atom.workspace.activePaneItem
   selection = editor.getSelection()
   lines = selection.getText().split "\n"
   for line in lines
   out += "- " + line + "\n"
   console.log(lines)
   selection.insertText(out)

这是我的 index-spec.coffee

{WorkspaceView} = require 'atom'

    describe "Markdown Utilities", ->
      [editor, editorView] = []

      unorderedList = (callback) ->
        editorView.trigger "md-utils:unorderedList"
        runs(callback)


      beforeEach ->

        atom.workspaceView = new WorkspaceView
        atom.workspaceView.openSync()

        editorView = atom.workspaceView.getActiveView()
        editor = editorView.getEditor()

      describe "when text is selected", ->
        it "formats it correctly", ->
          console.log = jasmine.createSpy("log")
          editor.setText """
            a
            b
            c
            d
          """
          editor.selectAll()
          unorderedList ->
            expect(console.log).toHaveBeenCalled()
            expect(editor.getText()).toBe """
            - a
            - b
            - c
            - d
          """

现在,当我运行规范时,似乎 index.coffee 中的方法甚至没有被调用。两个期望都失败了:

  • 预期的间谍日志已被调用。
  • 预期“abc d”为“-a -b -c -d”

该方法本身有效,所以我不明白为什么测试失败。非常感谢任何建议

4

1 回答 1

0

实际上,您的规格缺少包激活,通常是通过以下方式完成的:

beforeEach ->

  atom.workspaceView = new WorkspaceView
  atom.workspaceView.openSync()

  editorView = atom.workspaceView.getActiveView()
  editor = editorView.getEditor()

  # Package activation is done within a promise so this will wait for the end of it
  # before actually running the tests.
  waitsForPromise -> atom.packages.activatePackage('your-package-name')

由于您的包永远不会被激活,您在activate方法中定义的命令永远不会被注册,因此在您的助手中触发的事件unorderedList永远不会到达它。

于 2014-09-26T10:33:49.713 回答