【问题标题】:Rspec check if a block is yield with correct argumentsRspec 检查一个块是否具有正确的参数
【发布时间】:2016-08-25 09:03:22
【问题描述】:

我想检查 status_params 是否通过以及是否正确:

  def send_push_notification(order, parse_events)
    Parse.publish_status(order.deliverer_id) do
      status_params(
        parse_events[:role],
        order,
        generate_message(message)
      )
    end
  end

  def status_params(role, order, message)
    {
      role: role,
      status: order.status,
      order_id: order.id,
      alert: message
    }
  end

到目前为止,我的 rspec:

      it 'sends push notification to the deliverer when the order is ready' do
        expect(Parse).
          to receive(:publish_status).with(order.deliverer_id).once
        subject.update_order(
          order,
          status: 'ready'
        )
      end

所以它只检查deliverer_id 是否通过。块呢?我需要确保这些是正确的论点。请帮忙!

【问题讨论】:

  • 如果块只是调用一个返回哈希的方法,为什么要使用块呢?为什么不将哈希作为参数传递给Parse.publish_status
  • 您试图测试作为块传递的参数的事实让我认为那里存在一些设计问题。
  • @lcguida 你是什么意思?可以指定吗?

标签: ruby-on-rails ruby ruby-on-rails-4 rspec


【解决方案1】:

您可以使用block implementation 自己调用传递的块以检查其结果:

expect(Parse).to receive(:publish_status).with(order.deliverer_id).once do |&block|
  result = block.call
  expect(result).to be_a(Hash)
  expect(result[:status]).to eq('ready')
  # ...
end

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多