2016-10-21 24 views
0

我有一個模型測試belongs_to的包括由關注

class Survey < ActiveRecord::Base 
    include Respondable 
end 

在DB 調查桌子旁邊的字段:

  • responses_saved_date
  • responses_saved_by_type
  • responses_saved_by_id

和模塊

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 

我嘗試寫能夠響應關注單元測試,單獨進行測試,根據這一博客帖子https://semaphoreci.com/community/tutorials/testing-mixins-in-isolation-with-minitest-and-rspec

這裏是我的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 

當我運行測試我收到下一個錯誤:

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 

回答

2

I t hink如果你寫

class DummySurvey < Survey 

,而不是

class DummySurvey < ActiveRecord::Base 

你應該是金色的。當你定義一個從ActiveRecord::Base繼承的類時,你的期望是你的數據庫有正確的表和列(通過遷移設置,並由Rails命名約定確定)。

Rails應該在測試運行後重置測試數據庫,因此使用與您的實際應用程序相同的模型類是可行的。

0

也許一些方法來測試它孤立是:

describe Respondable do 
    class DummySurvey 
    attr_accessor :responses_saved_date, :responses_saved_by 
    def save 
     @saved = true 
    end 

    def persisted? 
     @saved 
    end 

    def self.belongs_to(association, options = {}) 
     @associations ||= {} 
     @associations[association] = OpenStruct.new(active_record: true, macro: :belongs_to, 
     options: options, foreign_key: options[:foreign_key] || "#{association}_id") 
    end 

    def self.reflect_on_association(association) 
     @associations ||= {} 
     @associations[association] 
    end 

    def self.column_names 
     ['responses_saved_by_type', 'responses_saved_by_id'] 
    end 

    include Respondable 
    end 

    subject { DummySurvey.new } 
    let(:user) { Object.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 
    subject.should be_persisted 
    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 
    subject.should be_persisted 
    end 
end 

另外,您可以創建數據庫表中的測試DummySurvey類或設置存在的表的table_name

class DummySurvey < ActiveRecord::Base 
    self.table_name = 'surveys' 
    include Respondable 
end