2013-06-19 2 views
3

모델 주소 및 하나의 모듈 Addressable이 모듈을 포함하는 AR 클래스에 대해 * belongs_to : address * 관계를 삽입 할 수 있습니다.rspec 테스트에서 임시 테이블을 만드는 방법

이 모듈을 포함하는 클래스가이 관계를 갖고 있는지 테스트하고 싶습니다.

클래스 주소 :

class Address < ActiveRecord::Base 
    attr_accessible :street, :zip 
end 

모듈 어드레스로

module Addressable 
    def self.included(base) 
    base.class_eval <<-CLASS_METHODS 
      belongs_to :address 
      validates :address, presence: true 
    CLASS_METHODS 
    end 
end 

테스트 코드 :

require 'spec_helper' 
describe 'Addressable' do 

    subject do 
    class OtherModel < ActiveRecord::Base 
     include Addressable 
    end 
    end 

    before(:all) do 
    connection = ActiveRecord::Base.connection 
    connection.create_table :othermodels do |t| 
     t.references :address 
    end 
    end 

    after(:all) do 
    connection.drop_table :othermodels 
    end 

    it "should validate presence of address"do 
    should validate_presence_of(:address) 
    end 

    it "should belongs to address" do 
should belong_to :continent 
    end 
end 

내 테스트가 실패

1) Addressable should validate presence of address 
    Failure/Error: should validate_presence_of(:address) 
    NoMethodError: 
    undefined method `address=' for OtherModel(Table doesn't exist):Class 

전 (: all)이 그 일을하지 않기를 바란다. 이 테이블을 만들려면 어떻게해야합니까?

답변

0

주소 지정이 가능한 모델에 테이블에 대한 OtherModel 정의에 테이블 이름을 명시 적으로 설정할 수 있습니다.

table_name = "some table that exists with addressable columns" 
관련 문제