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

2つのオプションがあります。

1.argsForCallスパイプロパティを使用する

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 に答える