【发布时间】:2014-10-30 03:57:43
【问题描述】:
我有一个 RSpec 测试套件,可以测试很多东西,包括创建一个名为 CompanyReportByReturnOnCapital 的模型。由于 Rails 的自动复数形式,关联的表曾经被称为 company_report_by_return_on_capitals,并且我已经运行迁移以将表重命名为 company_reports_by_return_on_capital。
但是,当我运行 rake 运行测试套件时,我收到以下错误:
1) StockReader#create_company_reports_by_roc creates reports
Failure/Error: CompanyReportByReturnOnCapital.delete_all
ActiveRecord::StatementInvalid:
PG::UndefinedTable: ERROR: relation "company_report_by_return_on_capitals" does not exist
LINE 1: DELETE FROM "company_report_by_return_on_capitals"
^
: DELETE FROM "company_report_by_return_on_capitals"
# ./spec/lib/stock_reader_spec.rb:102:in `block (3 levels) in <top (required)>'
我的 schema.rb:
ActiveRecord::Schema.define(version: 20141029193309) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "company_reports_by_earnings_yield", force: true do |t|
t.string "name"
t.string "symbol"
t.string "market_cap"
t.datetime "created_at"
t.datetime "updated_at"
t.string "ebit"
t.string "ebit_date"
t.string "enterprise_value"
t.string "earnings_yield"
t.string "fixed_assets"
t.string "working_capital"
t.string "return_on_capital"
t.string "market_cap_date"
t.string "total_assets"
t.string "total_assets_date"
t.string "current_assets"
t.string "current_assets_date"
t.string "total_debt"
t.string "total_debt_date"
t.string "cash_and_equivalents"
t.string "cash_and_equivalents_date"
t.string "working_capital_date"
end
create_table "company_reports_by_return_on_capital", force: true do |t|
t.string "name"
t.string "symbol"
t.string "ebit"
t.string "ebit_date"
t.string "market_cap"
t.string "market_cap_date"
t.string "working_capital"
t.string "working_capital_date"
t.string "total_assets"
t.string "total_assets_date"
t.string "current_assets"
t.string "current_assets_date"
t.string "total_debt"
t.string "total_debt_date"
t.string "cash_and_equivalents"
t.string "cash_and_equivalents_date"
t.string "fixed_assets"
t.string "enterprise_value"
t.string "earnings_yield"
t.string "return_on_capital"
t.datetime "created_at"
t.datetime "updated_at"
end
end
我的迁移文件重命名表:
class CreateCompanyReportByReturnOnCapitals < ActiveRecord::Migration
def change
create_table :company_report_by_return_on_capitals do |t|
t.string :name
t.string :symbol
t.string :ebit
t.string :ebit_date
t.string :market_cap
t.string :market_cap_date
t.string :working_capital
t.string :working_capital_date
t.string :total_assets
t.string :total_assets_date
t.string :current_assets
t.string :current_assets_date
t.string :total_debt
t.string :total_debt_date
t.string :cash_and_equivalents
t.string :cash_and_equivalents_date
t.string :fixed_assets
t.string :enterprise_value
t.string :earnings_yield
t.string :return_on_capital
t.timestamps
end
end
end
class RenameCompanyReportByReturnOnCapitals < ActiveRecord::Migration
def change
rename_table :company_report_by_return_on_capitals, :company_report_by_return_on_capital
end
end
class RenameReturnOnCapitalReports < ActiveRecord::Migration
def change
rename_table :company_report_by_return_on_capital, :company_reports_by_return_on_capital
end
end
这里是 spec/lib/stock_reader_spec.rb 中的测试:
require 'spec_helper'
require 'stock_reader'
RSpec.describe StockReader do
describe '#create_company_reports_by_roc' do
before(:each) do
CompanyReportByReturnOnCapital.delete_all
end
it 'creates reports' do
StockReader.create_company_reports_by_roc($return_on_capital_array)
expect(CompanyReportByReturnOnCapital.count).to eq(2)
end
end
end
最后是实际代码所在的 lib/stock_reader.rb:
require 'csv'
require 'httparty'
module StockReader
def self.create_company_reports_by_roc(company_data_array)
company_data_array.each do |company|
CompanyReportByReturnOnCapital.create(
symbol: company[:symbol],
enterprise_value: company[:enterprise_value].to_s,
enterprise_value_date: company[:enterprise_value_date],
ebit: company[:ebit].to_s,
ebit_date: company[:ebit_date],
earnings_yield: company[:earnings_yield].to_s)
end
end
end
到目前为止,我已经尝试使用以下命令重置测试数据库:
RAILS_ENV=test rake db:drop db:create db:migrate、rake db:test:load、rake db:test:prepare
AND 开发数据库使用:
bin/rake db:drop 和 bin/rake db:create 和 bin/rake db:migrate 没有成功。
我没有想法!
为了更好的衡量,我将包括我的模型,app/models/company_report_by_return_on_capital.rb:
class CompanyReportByReturnOnCapital < ActiveRecord::Base
end
提前致谢
【问题讨论】:
标签: ruby-on-rails ruby rspec