Tintin81
Tintin81

Reputation: 10207

Why do my Rspec tests fail after Rails 4 upgrade?

I recently upgraded my Rails application to Rails 4 and in my Rspec test suite one line is causing an error:

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 (?, ?, ?, ?, ?, ?, ?, ?, ?)

This is my code:

# /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

Can anybody tell me what I am missing here?

I guess it's somehow related to the fact that the Invoice class accepts nested items.

Upvotes: 1

Views: 803

Answers (1)

Jeffery Utter
Jeffery Utter

Reputation: 143

Your problem may have something to do with the Faker Gem, although i don't think it would looking at your code. I'm not sure what has changed in rails4 that would cause any problems with Faker (or factory_girl for that matter)

I had a factory like this:

FactoryGirl.define do
  factory :job, :class => Job do
    ...
    password        { Faker::Lorem.words()[0..38] }
    ...
  end
end

This caused the same error (only in rails 4 - worked fine in 3) until I changed it to "Faker::Lorem.words().join()[0..38].to_s"

Upvotes: 4

Related Questions