lundi 19 octobre 2015

`object.message if object.repond_to? :message` works, but is prooving difficult to test

I have the following setup in a rails app:

class Song < ActiveRecord::Base

  after_commit :update_singable_index
  belongs_to :singable, polymorphic: true

  def update_singable_index
    singable.update_index if singable.respond_to? :update_index
  end
end

I've redacted the actual class heavily. Please let me know if any more information is needed.

But basically the Song model has a polymorphic association with a bunch of models. Some of them have elasticsearch indices and some of them don't. As a result some of them will accept the update_index message, while others will throw an NoMethodError: undefined method 'update_index'

Then in the test I have the following setup:

RSpec.describe Song, type: :model do
  describe '.update_index_of_updatable' do
    it 'updates Updatable\'s index' do
      song = create(:song, :for_updatable)
      updatable = song.singable

      expect(updatable).to receive(:update_index)

      song.save
    end

    it 'doesn\'t attempt to update an Unupdatable\'s index' do
      song = create(:song, :for_unupdatable)
      unupdatable = song.singable

      expect(unupdatable).not_to receive(:update_index)

      song.save
    end
  end
end

The first test does indeed pass. The second test on the other hand fails

1) Song.update_singable_index doesn't attempt to update an Unupdatable's index
     Failure/Error: update.save
       (#<Unupdatable:0x0000000b39be98>).update_index(no args)
           expected: 0 times with any arguments
           received: 1 time
     # ./app/models/song.rb:103:in `update_index_of_updatable'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:430:in `block in make_lambda'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:261:in `call'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:261:in `block in simple'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `block in call'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `each'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/bugsnag-2.8.12/lib/bugsnag/rails/active_record_rescue.rb:8:in `run_callbacks'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:314:in `committed!'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:89:in `commit_records'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:47:in `test_commit_records'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:23:in `block in transaction_with_transactional_fixtures'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:9:in `transaction_with_transactional_fixtures'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:286:in `block in save'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:285:in `save'
     # ./spec/models/song_spec.rb:141:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:417:in `block (2 levels) in <top (required)>'
     # ./spec/rails_helper.rb:192:in `block (2 levels) in <top (required)>'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:52:in `block (3 levels) in apply'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:43:in `times'
     # /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:43:in `block (2 levels) in apply'

Aucun commentaire:

Enregistrer un commentaire