Ruby on rails 3 与factory_girl、Desive、rpsec 2.0和rails 3.0有关的问题=>;can';t让rspec验证控制器

Ruby on rails 3 与factory_girl、Desive、rpsec 2.0和rails 3.0有关的问题=>;can';t让rspec验证控制器,ruby-on-rails-3,devise,rspec2,Ruby On Rails 3,Devise,Rspec2,github上有一些ressource。 但是how-to是非常高的级别,我不能让它在我的环境中工作。 将所有这些部件插入或配置在一起以测试需要登录用户的控制器的正确方法是什么(在_filter:authenticate _user!) 目前,我尝试在单个控制器上运行rspec require File.dirname(__FILE__) + '/../spec_helper' describe ArticlesController do fixtures :all render_vie

github上有一些ressource。 但是how-to是非常高的级别,我不能让它在我的环境中工作。 将所有这些部件插入或配置在一起以测试需要登录用户的控制器的正确方法是什么(在_filter:authenticate _user!)

目前,我尝试在单个控制器上运行rspec

require File.dirname(__FILE__) + '/../spec_helper'
describe ArticlesController do
  fixtures :all
  render_views

  before (:each) do
    @user = Factory.create(:user)
    sign_in @user
  end

  it "index action should render index template" do
    get :index
    response.should render_template(:index)
  end
end
这是我运行rspec时的输出

Failures:
  1) ArticlesController index action should render index template
     Failure/Error: Unable to find matching line from backtrace
     SQLite3::ConstraintException: articles.user_id may not be NULL

看起来你的装置有问题。错误消息表示它无法创建没有用户id的文章

您可以修复这些装置,也可以通过删除
装置:all
并删除find方法来避免使用它们:

before(:each) do
  @user = Factory.create(:user)
  sign_in @user
  Article.stub(:find).and_return([])
end

这将告诉
find
返回一个空数组,控制器索引操作应将该数组分配给@articles实例变量,以便在模板中使用。这应该足以使模板呈现无误。

我修复了夹具,或者应该这样说工厂:

Factory.define :article do |e|
  e.name "Article001"
  e.user { |u| u.association(:user) }
end
这给了我一个新的错误

Failures:
  1) ArticlesController index action should render index template
     Failure/Error: response.should render_template(:index)
     expecting <"index"> but rendering with <"devise/mailer/confirmation_instructions">.
     Expected block to return true value.
故障:
1) ArticlesController索引操作应呈现索引模板
失败/错误:response.should呈现_模板(:index)
应为,但渲染为。
预期块将返回真值。
我只想看到我的测试通过我的文章控制器的索引方法。我认为我对用户创建不感兴趣。错误是告诉我已经创建了一个用户

我试过你的建议,但以“存根”结束

失败/错误:Asset.stub(:find).和_return([])
未定义的方法“存根”#
fixture:all
和您的方法之间的最佳方法是什么

Failure/Error: Asset.stub(:find).and_return([])
     undefined method `stub' for #<Class:0x000000048310b8>