Rails夹具未随rspec一起加载 [英] Rails Fixtures not loading with rspec

查看:52
本文介绍了Rails夹具未随rspec一起加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试在Rails项目的上下文中学习rspec BDD测试框架.我遇到的问题是,我一生无法将自己的装置正确地加载到rspec描述中.

So, I'm trying to learn the rspec BDD testing framework in the context of a rails project. The problem I'm having is that I can't, for the life of me, get my fixtures to load properly in rspec descriptions.

免责声明:是的,还有比固定装置更好的东西.我尝试一次学习一件事,在这里(特别是rspec),然后再使用工厂工具,摩卡咖啡,自动测试等相关工具. ,如果笨重,则灯具可以正常工作.

Disclaimer: Yes, there are better things than fixtures to use. I'm trying to learn one thing at a time, here (specifically rspec) before I go play with associated tools like factory-girl, mocha, auto-test, etc. As such, I'm trying to get the dead-simple, if clunky, fixtures working.

无论如何,这是代码:

/test/fixtures/users.yml-

/test/fixtures/users.yml -

# password: "secret"
foo:
  username: foo
  email: foo@example.com
  password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
  password_salt: bef65e058905c379436d80d1a32e7374b139e7b0

bar:
  username: bar
  email: bar@example.com
  password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
  password_salt: bef65e058905c379436d80d1a32e7374b139e7b0

/spec/controllers/pages_controller_spec.rb-

/spec/controllers/pages_controller_spec.rb -

require 'spec/spec_helper'

describe PagesController do
  integrate_views
  fixtures :users
  it "should render index template on index call when logged in" do
    session[:user_id] = user(:foo).id
    get 'index' 
    response.should render_template('index')
  end
end

当我运行"rake spec"时,我得到的是:

And what I'm getting when I run 'rake spec' is:

NoMethodError in 'PagesController should render index template on index call when logged in'
undefined method `user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0x2405a7c>
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/test_process.rb:511:in `method_missing'
./spec/controllers/pages_controller_spec.rb:7:

也就是说,它没有将'user(:foo)'识别为有效方法.

That is, it's not recognizing 'user(:foo)' as a valid method.

这些夹具本身必须没问题,因为当我通过"rake db:fixtures:load"将它们加载到开发数据库中时,我可以验证该数据库中是否存在foo和bar.

The fixtures themselves must be ok, since when I load them into the development db via 'rake db:fixtures:load', I can verify that foo and bar are present in that db.

我觉得我在这里缺少明显的东西,但是我整天都在扯头发,无济于事.任何帮助将不胜感激.

I feel like I'm missing something obvious here, but I've been tearing my hair out all day to no avail. Any help would be appreciated.

推荐答案

如果将灯具定义为用户",则使用灯具的方法是使用同名的方法:

If you define fixtures as 'users', then the way to use them is via the method with the same name:

describe PagesController do
  integrate_views
  fixtures :users
  it "should render index template on index call when logged in" do
    session[:user_id] = users(:foo).id
    get 'index'
    response.should render_template('index')
  end
end

单数仅与类本身(用户)相关.如果这只是一个字母错误,希望您还剩下一些头发.

The singular is only relevant to the class itself (User). Hope you still have some hair left if this is just a one letter bug.

这篇关于Rails夹具未随rspec一起加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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