モデル Address と、このモジュール Addressable を含む AR クラスの *belongs_to :address* リレーションを注入する 1 つのモジュール Addressable があります。
このモジュールを含むクラスがこの関係を持っていることをテストしたいと思います。
クラス住所:
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
before(:all) が機能していないと思います。このテーブルを作成するにはどうすればよいですか。