【问题标题】:Rspec: How to test that on every each loop class method is called?Rspec:如何在每个循环类方法被调用时测试它?
【发布时间】:2019-01-24 22:43:37
【问题描述】:

我目前正在尝试在我的 rails worker 类中测试一部分代码,如下所示(简化版);

class SenderWorker
   include Sidekiq::Worker
   sidekiq_options :retry => 5

   def perform(current_user_guid)
      Rails.logger.info "Starting for user_guid: #{current_user_guid}"
      user = User.find_by!(guid: current_user_guid)
      team = Team.find_by!(uuid: user.team.uuid)
      profiles = team.profiles 
      profiles.each do |profile|
         SenderClass.new(profile,
                         user).send(User::RECALL_USER)
      end
      Rails.logger.info "Finishing for user_guid: #{current_user_guid}"
   end
end

我写的测试就是这些,它们都通过了;

context 'when something occurs' do
  it 'should send' do
    sender = double("sender")
    allow(SenderClass).to receive(:new).with(user_profile, current_user) { sender }
    expect(sender).to receive(:send)
    expect(Rails.logger).to receive(:info).exactly(2).times

    worker.perform(user.guid)
  end
end

但是,我并未测试所有呼叫。有没有办法确保我测试 each do 循环中调用的所有内容。提前谢谢你。

【问题讨论】:

    标签: ruby-on-rails testing rspec tdd


    【解决方案1】:

    您可以测试:send 是否收到了预期的次数。

    但我建议您通过使用类方法来封装那些链接的方法来简化测试。比如:

    def self.profile_send(profile, user)
      new(profile, user).send(User::RECALL_USER)
    end
    

    然后:

    def perform(current_user_guid)
      Rails.logger.info "Starting for user_guid: #{current_user_guid}"
      user = User.find_by!(guid: current_user_guid)
      team = Team.find_by!(uuid: user.team.uuid)
      profiles = team.profiles 
      profiles.each do |profile|
        SenderClass.profile_send(profile, user)
      end
      Rails.logger.info "Finishing for user_guid: #{current_user_guid}"
    end
    

    现在您可以测试 SenderClass 收到 :send_profile X 次了。

    如果你真的想测试 new 和 send 方法调用,你可以为 SenderClass.send_profile 添加一个测试,但是你可以在循环之外测试一次,这两个测试都会覆盖你想要的.

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多