我有一个模型地址和一个模块可寻址为包含此模块可寻址的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
我认为之前(:all)没有做它的工作。我该如何创建这个表。
答案 0 :(得分:0)
您可以尝试在OtherModel定义中显式地将表名设置为模型中存在的可寻址的表。
table_name = "some table that exists with addressable columns"
答案 1 :(得分:0)
晚了四年,但我想测试一下类似的东西并找到了您的问题。我认为您一切正常,只不过表应按惯例称为other_models
(您的类为OtherModel
)。