Rail 3.2.2/Devise: deprecation warning with rspec
Asked Answered
B

3

14

I recently upgraded an app to rails 3.2.2.

I'm using Factory_girl

Factory.sequence :name do |n| "name-#{n}" end

Factory.define :user do |u| u.first_name{ Factory.next(:name) }
u.last_name { |u| 'last_' + u.first_name } u.password 'secret'
u.password_confirmation { |u| u.password } u.sequence(:email) { |i| "user_#{i}@example.com" }

end

and this simple test

specify { Factory.build(:user).should be_valid }

generate the following warning

DEPRECATION WARNING: You're trying to create an attribute user_id'. Writing arbitrary attributes on a model is deprecated. Please just use attr_writer` etc. (called from block (2 levels) in at...

How can I get rid of it?

Bred answered 3/3, 2012 at 7:26 Comment(0)
F
19

It's probably because you haven't prepared/migrated your test database with updated column definitions, thus it thinks you're trying to arbitrarily set the attribute.

Run rake db:test:prepare to make sure it's updated.

Here's the source code of that method, where you can see Rails checks for the column or attribute first, then warns if they're not found.

Fideicommissary answered 9/3, 2012 at 22:7 Comment(2)
unfortunately it is not the issue. my test db is up to date. I've run the prepare task and I still have the same warningBred
Sorry to hear that. Sometimes I have to run RAILS_ENV=test rake db:migrate to actually migrate it. Just an idea, check the actual test db to ensure the columns are present.Fideicommissary
C
4

I've met the same warning with the following code:

Ad model:

class Ad < ActiveRecord::Base
    belongs_to :user
end

Factories:

FactoryGirl.define do 
    factory :ad do
        association :user
    end
end

FactoryGirl.define do 
    factory :user do
        first_name {Factory.next(:first_name)}
        last_name {Factory.next(:last_name)}
        email {|x| "#{x.first_name}.#{x.last_name}#{Factory.next(:count)}@test.com"}
        password Forgery(:basic).password
        confirmed_at Date.today << 10
    end
end

Test

require 'spec_helper'

describe Ad do
    before(:each) do
        @ad = Factory.build(:ad)
    end

    "it is not valid without a user"
end

Running the test gave me a similar error.

Adding

attr_accessor :user

to the Ad model fixed the warning.

I hope it helps.

Congreve answered 8/3, 2012 at 10:8 Comment(1)
Worked for me without Devise. Thanks, it was driving me crazy.Immobilize
A
0

I had this same warning while doing tests in Rspec and my issue was that I had a Parent model and Child model where I accidentally had this:

class Child < ActiveRecord::Base
  belongs_to :parent
end

......

class Parent < ActiveRecord::Base
  belongs_to :child
end
Alehouse answered 27/8, 2013 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.