2013-06-19 51 views
3

創建時間的表有一個模型地址和一個模塊尋址注入* belongs_to的:對於AR類,其中包括該模塊可尋址地址*關係。我如何在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 

我認爲之前(:全部)沒有做它的工作。我怎樣才能創建這個表格。

回答

0

你可以嘗試在OtherModel定義明確設置的表名存在於你的模型具有可尋址的表。

table_name = "some table that exists with addressable columns"