0

我怎样才能告诉 Jasmine 间谍只听我告诉它期待的消息而忽略任何其他消息?

例如:

示例组

describe 'View', ->
  describe 'render', ->
    beforeEach ->
      @view    = new View
      @view.el = jasmine.createSpyObj 'el', ['append']
      @view.render()

    it 'appends the first entry to the list', ->
      expect(@view.el.append).toHaveBeenCalledWith '<li>First</li>'

    it 'appends the second entry to the list', ->
      expect(@view.el.append).toHaveBeenCalledWith '<li>Second</li>'

执行

class View
  render: ->
    @el.append '<li>First</li>', '<li>Second</li>'

输出

View
  render
    appends the first entry to the list
      Expected spy el.append to have been called \
        with [ '<li>First</li>' ] but was called \
        with [ [ '<li>First</li>', '<li>Second</li>' ] ]

    appends the second entry to the list
      Expected spy el.append to have been called \
        with [ '<li>Second</li>' ] but was called \
        with [ [ '<li>First</li>', '<li>Second</li>' ] ]
4

2 回答 2

1

有两种选择:

1.使用argsForCallspy属性

it 'appends the first entry to the list', ->
   expect(@view.el.append.argsForCall[0]).toContain '<li>First</li>'

2.使用对象的args属性mostRecentCall

it 'appends the first entry to the list', ->
   expect(@view.el.append.mostRecentCall.args).toContain '<li>First</li>'
于 2012-08-01T12:51:02.150 回答
0

需要明确的是,你不能阻止间谍听。间谍将监听所有函数调用并保存它们。但是您可以使用argsForCall来超出每个调用。

于 2012-08-02T20:08:44.747 回答