2012-03-03 56 views
14

我最近将应用程序升级到了rails 3.2.2。Rail 3.2.2/Devise:rspec的弃用警告

我使用Factory_girl

Factory.sequence:名称做| 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”}

和这个简单的测试

指定{Factory.build(:用户)。应该be_valid}

产生以下警告

弃用警告:你想创建一个属性user_id'. Writing arbitrary attributes on a model is deprecated. Please just use attr_writer`等(来自框称为(2级)在 在...

我怎样才能摆脱它?

回答

4

我见过用下面的代码相同的警告:

广告模式:

class Ad < ActiveRecord::Base 
    belongs_to :user 
end 

工厂:

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 

测试

require 'spec_helper' 

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

    "it is not valid without a user" 
end 

运行测试给了我一个类似的错误。

添加

attr_accessor :user 

的广告模式固定的警告。

我希望它有帮助。

+0

它没有为我工作。任何其他想法? – Alpha 2012-03-12 06:21:34

+0

没有设计就为我工作。谢谢,这让我疯狂。 – IAmNaN 2012-04-22 20:11:48

19

这可能是因为你没有准备/迁移你的测试数据库更新列定义,因此它认为你试图任意设置属性。

运行rake db:test:prepare以确保它已更新。

Here's the source code该方法,您可以看到Rails首先检查列或属性,然后警告是否找不到它们。

+0

不幸的是这不是问题。我的测试数据库是最新的。我已经运行了准备任务,我仍然有相同的警告 – Alpha 2012-03-20 05:35:39

+1

很抱歉听到。有时我必须运行'RAILS_ENV = test rake db:migrate'来实际迁移它。只是一个想法,检查实际的测试数据库,以确保列存在。 – trisweb 2012-03-20 17:29:15

0

我有同样的警告,而在Rspec的做测试,我的问题是,我不得不在这里我不小心有这个父模型和子模型:

class Child < ActiveRecord::Base 
    belongs_to :parent 
end 

...... 

class Parent < ActiveRecord::Base 
    belongs_to :child 
end