1

github上有一些资源How To: Test (devise) with Rails 3 and RSpec。但是如何做是非常高级的,我不能让它在我的上下文中工作。将所有这些部分插入或配置在一起以设法测试需要登录用户的控制器的正确方法是什么(before_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
4

2 回答 2

0

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

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.

我只是想看看我的测试我的文章控制器的索引方法是否通过。我对用户创建不感兴趣。错误告诉我已经创建了一个用户。

我尝试了您的建议,但停止使用“存根”

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

和你的方法之间最好的方法是fixture:all什么?

于 2010-11-02T18:10:40.833 回答
0

看起来你的灯具有错误。错误消息是说它无法创建没有 user_id 的文章。

您可以修复固定装置,或者通过删除fixtures :all并存根 find 方法来避免使用它们:

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

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

于 2010-11-02T06:34:03.977 回答