我最近将我的Rails应用程序升级到Rails 4,在我的Rspec测试套件中,一行导致错误:
ActiveRecord::StatementInvalid:
TypeError: can't cast Array to string: INSERT INTO "items" ("created_at", "date", "description", "invoice_id", "position", "price_in_cents", "quantity", "tax_rate", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
这是我的代码:
# /spec/controllers/invoices_controller_spec.rb:
require 'spec_helper'
describe InvoicesController do
before :each do
...
@invoice = FactoryGirl.create(:invoice, :project_id => @project.id, :user => @user, :items_attributes => [ FactoryGirl.attributes_for(:item) ]) # causing havoc
end
...
end
# /spec/factories.rb:
factory :invoice do
number 123
recipient { Faker::Name.name }
date { Time.zone.now.to_date }
association :user
association :project
factory :invoice_with_item do
items { |i| [i.association(:item)] }
end
end
有人可以告诉我这里缺少什么吗?
我想这与Invoice
类接受嵌套项目的事实有某种关系。
答案 0 :(得分:4)
你的问题可能与Faker Gem有关,虽然我不认为它会查看你的代码。我不确定rails4中有什么变化会导致Faker(或者factory_girl)出现任何问题
我有一个这样的工厂:
FactoryGirl.define do
factory :job, :class => Job do
...
password { Faker::Lorem.words()[0..38] }
...
end
end
这导致了同样的错误(仅在rails 4中 - 在3中正常工作),直到我将其更改为“Faker :: Lorem.words()。join()[0..38] .to_s”