首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在rspec测试中创建时态表

如何在rspec测试中创建时态表
EN

Stack Overflow用户
提问于 2013-06-19 23:11:02
回答 2查看 1.8K关注 0票数 4

我有一个模型地址和一个模块可寻址,为包括这个模块可寻址的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)没有完成它的工作。如何创建此表。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17194884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档