我正在尝试为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中的方法甚至没有被调用。 两个期望都失败了:
该方法本身有效,所以我不明白为什么测试失败。 任何建议都非常感谢
答案 0 :(得分: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
帮助程序中触发的事件永远不会到达。