工厂女孩 + RSpec + Rails 3 '未定义方法<属性>='

发布于 2024-10-16 23:20:18 字数 1539 浏览 2 评论 0原文

我对 Rails 和 TDD 相当陌生(毫无疑问,从我的帖子中可以明显看出),而且我很难理解 Rspec 和 FactoryGirl。

我正在使用 Rails 3、rspec 和 Factory Girl:

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

我有一个用户模型,在开发过程中已成功运行测试,但随后需要添加一个名为“source”的属性。它用于确定用户记录最初来自哪里(本地还是 LDAP)。

在我的factory.rb文件中,我定义了几个工厂,它们看起来像下面这样:

# 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

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

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

我在多个规范中调用它,如下所示(示例来自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”属性,一切正常,但是一旦我这样做,我就会收到这样的错误,在运行测试时

  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 看到了我的模型的旧版本,但没有意识到我已经向它添加了属性。但是,如果我将以下行放入我的用户模型中,错误就会消失

attr_accessor :source

......这表明它实际上正在查看正确的模型。

帮助!

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.

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

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

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).

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

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

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>

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.

Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔小萌 2024-10-23 23:20:18

运行这个怎么样?

rake db:test:load

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

How about running this?

rake db:test:load

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

二智少女猫性小仙女 2024-10-23 23:20:18

如果您不使用 schema.rb (例如您设置了 config.active_record.schema_format = :sql)
你应该跑

rake db:test:prepare

if you don't use schema.rb (e.g. you have set config.active_record.schema_format = :sql)
you should run

rake db:test:prepare
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文