FactoryGirl + RSpec + Rails 3'未定义的方法< attribute> =' [英] FactoryGirl + RSpec + Rails 3 'undefined method <attribute>='

查看:67
本文介绍了FactoryGirl + RSpec + Rails 3'未定义的方法< attribute> ='的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rails和TDD还是很陌生(毫无疑问,这在我的帖子中会很明显),并且我很难将自己的脑筋围绕在Rspec和FactoryGirl上.

I'm fairly new to rails and TDD (as will no doubt be obvious from my post) and am having a hard time wrapping my brain around Rspec and FactoryGirl.

我正在使用Rails 3,rspec和factory girl:

I'm using Rails 3, rspec and factory girl:

gem 'rails', '3.0.3'
# ...   
gem 'rspec-rails', '~>2.4.0'
gem 'factory_girl_rails'

我有一个用户模型,我已经在开发过程中成功进行了测试,但是随后需要向其中添加一个称为源"的属性.它用于确定用户记录的原始来源(本地与LDAP).

I have a user model that I've been successfully running tests on during development, but then needed to add an attribute to, called "source". It's for determining where the user record originally came from (local vs LDAP).

在我的factory.rb文件中,我定义了几个工厂,看起来类似于以下内容:

In my factories.rb file, I have several factories defined, that look something like the following:

# An alumnus account tied to LDAP
Factory.define :alumnus, :class => User do |f|
  f.first_name "Mickey"
  f.last_name  "Mouse"
  f.username   "mickeymouse"
  f.password   "strongpassword"
  f.source     "directory"
end

我定义了一个宏(到目前为止一直在工作),看起来像这样:

I have a macro defined (that's been working up until now) that looks like this:

def login(user)
  before(:each) do
    sign_out :user
    sign_in Factory.create(user)
  end
end

我在多个规范中这样称呼它(例如,来自users_controller_spec.rb的示例):

I'm calling it in multiple specs like so (example from users_controller_spec.rb):

describe "for non-admins or managers" do
  login(:alumnus)

  it "should deny access" do
    get :index
    response.should redirect_to(destroy_user_session_path)
  end

end

如果我未指定"source"属性,则一切正常,但是一旦运行,在运行测试时就会收到类似的错误

If I don't specify the "source" attribute, everything works OK, but as soon as I do, I get an error like so when running the test

  12) UsersController for non-admins or managers should deny access
 Failure/Error: Unable to find matching line from backtrace
 NoMethodError:
   undefined method `source=' for #<User:0x00000100e256c0>

我可以从Rails控制台和应用程序本身访问属性,并且在用户模型的attr_accessible中列出了该属性.就像Rspec看到的是我的模型的旧版本,似乎没有意识到我已经为其添加了属性.但是,如果我在用户模型中加入以下代码,错误就会消失

I can access the attribute no problem from the rails console and the app itself, and it's listed in my attr_accessible in the user model. It's almost as though Rspec is seeing an old version of my model and not recognizing that I've added an attribute to it. But if I put the following line into my user model, the error disappears

attr_accessor :source

...这向我表明它实际上正在寻找正确的模型.

... which indicates to me that it is actually looking at the correct model.

帮助!

推荐答案

如何运行它?

rake db:test:load

[如果添加了新属性,则需要将其迁移到测试数据库.]

[If you added a new attribute you'd need to migrate it to the test database.]

这篇关于FactoryGirl + RSpec + Rails 3'未定义的方法&lt; attribute&gt; ='的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆