vendredi 21 octobre 2016

Test belongs_to included by concern

I have a model

class Survey < ActiveRecord::Base
  include Respondable
end

in DB surveys table have next fields:

  • responses_saved_date
  • responses_saved_by_type
  • responses_saved_by_id

and module

module Respondable
  extend ActiveSupport::Concern

  included do
    belongs_to :responses_saved_by,    :polymorphic => :true
  end

  def update_saved_settings!(person=nil)
    self.responses_saved_date = Time.now
    self.responses_saved_by   = person
    self.save
  end
end

I try to write unit tests for Respondable concern, to test it in isolation, according to this blogpost http://ift.tt/210W57Z

Here is my rspec:

describe Respondable do
  class DummySurvey < ActiveRecord::Base
    include Respondable
  end

  subject { DummySurvey.new }

  it { should belong_to(:responses_saved_by) }

  it 'saves the settings with user' do
    subject.update_saved_settings!(user)

    subject.responses_saved_date.should_not be_nil
    subject.responses_saved_by.should eq user
  end

  it 'saves the settings without user' do
    subject.update_saved_settings!

    subject.responses_saved_date.should_not be_nil
    subject.responses_saved_by.should be_nil
  end
end

When I run tests I receive next Error:

Failure/Error: subject { DummySurvey.new }
ActiveRecord::StatementInvalid:
    PG::UndefinedTable: ERROR:  relation "dummy_survey" does not exist
    LINE 5: WHERE a.attrelid = '"dummy_survey"'...
                                 ^
    : SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                         pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                    FROM pg_attribute a LEFT JOIN pg_attrdef d
                      ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                   WHERE a.attrelid = '"dummy_survey"'::regclass
                     AND a.attnum > 0 AND NOT a.attisdropped
                   ORDER BY a.attnum

Aucun commentaire:

Enregistrer un commentaire