1

问题

我必须为涉及与原子编辑器确认对话框交互的代码编写规范有哪些选项?

背景

我正在为 atom 开发一个包,并且有一个命令来删除一个文件,然后将更改推送到服务器。我想编写一个测试来验证命令的行为,但是在想出一个好方法来模拟单击确认对话框上的取消/确定按钮时遇到了麻烦

命令代码如下所示

atom.workspaceView.command "mavensmate:delete-file-from-server", =>
  # do setup stuff (build the params object)
  atom.confirm
    message: "You sure?"
    buttons:
      Cancel: => # nothing to do here, just let the window close
      Delete: => # run the delete handler
        @mm.run(params).then (result) =>
          @mmResponseHandler(params, result)

我似乎无法弄清楚如何让取消或删除回调在规范中运行。我一直在挖掘所有原子规格并搜索谷歌,但似乎没有任何结果。我希望将返回设置为我想要触发的回调的索引会起作用,但是我的删除按钮回调永远不会被调用。

# Delete the metadata in the active pane from the server
describe 'Delete File from Server', ->
  filePath = ''

  beforeEach ->
    # set up the workspace with a fake apex class
    directory = temp.mkdirSync()
    atom.project.setPath(directory)
    filePath = path.join(directory, 'MyClass.cls')
    spyOn(mm, 'run').andCallThrough()

    waitsForPromise ->
      atom.workspace.open(filePath)

  it 'should invoke mavensmate:delete-file-from-server if confirmed', ->
    spyOn(atom, 'confirm').andReturn(1)
    atom.workspaceView.trigger 'mavensmate:delete-file-from-server'
    expect(mm.run).toHaveBeenCalled()

有没有更好的方法来模拟用户单击确认对话框上的按钮?是否有任何解决方法可以对此进行测试?

4

1 回答 1

0

如果您使用按钮传递回调,似乎没有一种模拟与确认对话框交互的好方法,但是如果您只是传递一个数组,并让命令触发器响应它,那么您可以编写一个根据需要规范。

命令代码将如下所示

atom.workspaceView.command "mavensmate:delete-file-from-server", =>
  # do setup stuff (build the params object)
  atom.confirm
    message: "You sure?"
    buttons: ["Cancel", "Delete"]
  if answer == 1
    @mm.run(params).then (result) =>
      @mmResponseHandler(params, result)

并且该规范将在其当前版本中运行

# Delete the metadata in the active pane from the server
describe 'Delete File from Server', ->
  filePath = ''

  beforeEach ->
    # set up the workspace with a fake apex class
    directory = temp.mkdirSync()
    atom.project.setPath(directory)
    filePath = path.join(directory, 'MyClass.cls')
    spyOn(mm, 'run').andCallThrough()

    waitsForPromise ->
      atom.workspace.open(filePath)

  it 'should invoke mavensmate:delete-file-from-server if confirmed', ->
    spyOn(atom, 'confirm').andReturn(1)
    atom.workspaceView.trigger 'mavensmate:delete-file-from-server'
    expect(mm.run).toHaveBeenCalled()
于 2014-07-31T07:32:04.793 回答