我有一个模型地址和一个模块可寻址,为包括此模块可寻址的 AR 类注入 *belongs_to :address* 关系。
我想测试包含这个模块的那个类有这个关系。
上课地址:
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) 没有做它的工作。我怎样才能创建这个表。