Pergunta

I have a (simplified) module: (/app/jobs/checkpulse.rb)

module CheckPulse
  @queue = :heartbeat
  def self.perform()
    Machine.all.each do |machine|
      machine.update_columns(active_alarm: true) #do not want to touch updated_at
    end
  end
end

I can use it in the rails console and it changes all machines as expected.

I am trying to write a spec to test it: (/spec/jobs/checkpulse_spec.rb)

require "spec_helper"
require_relative '../../app/jobs/checkpulse'

describe "CheckPulse" do

  let(:user) { FactoryGirl.create(:user) }
  let!(:machine) { FactoryGirl.create(:machine, user: user, heartbeat_timeout: 5) }

  it "should change the machine to alarm mode" do
    CheckPulse.perform
    expect(machine.active_alarm).to be_true
  end
end

This test fails every time. I have used puts inside the CheckPulse module to check on it and it has the expected behaviour inside the module, but when it returns from the module, the machine has not changed.

I have tried mixing the module into a dummy class, but that didn't work either. I also tried running rails console in the test environment to see if it was an environment problem, but the module worked fine there too.

I have spent the entire day on this, please help!

Foi útil?

Solução

First of all, try to follow ruby & rails conventions. If you call module CheckPulse it's file name should be check_pulse.rb. It will help you to avoid require_relative. Second, in spec and in module you interact with different ruby objects which are both writing to same row in database. And if you update one object another will not be updated synchronously. You need to reload it manually by calling machine.reload in spec.

Also few tips to your code:

  • never use all.each. It will stuck if you have a 2k+ records in your database. For iterating all database records use find_each
  • instead of loading each record and update it, you could update all records with update_all -> Machine.update_all(:column_1 => value_1, :column_2 => value_3)
  • avoid round brackets in methods without arguments
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top