与factory_girl、devise、rpsec 2.0 和rails 3.0 相关的问题=>无法获取 rspec 来验证控制器

发布于 2024-09-30 04:59:43 字数 895 浏览 1 评论 0原文

github上有一些资源 如何目标:使用 Rails 3 和 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

there is some ressource on github How To: Test (devise) with Rails 3 and RSpec .
but the how to is very high level and I can't have it working in my context.
What is the proper way to plug or configure all those pieces together to managed to test a controller that require a logged user (before_filter :authenticate_user!) ???

For now I try to run rspec on a single controller..

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

here is the output when I run 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

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

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

发布评论

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

评论(2

听,心雨的声音 2024-10-07 04:59:43

看起来你的装置中有一个错误。错误消息表明它无法创建没有 user_id 的文章。

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

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

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

Looks like an error in your fixtures. The error message is saying that it can't create an Article without a user_id.

You can either fix the fixtures, or avoid using them by removing the fixtures :all and stubbing the find method:

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

This tells find to return an empty array, which your controller index action should assign to the @articles instance variable for use in the template. That ought to be enough to get the template rendered without errors.

め可乐爱微笑 2024-10-07 04:59:43

我修复了固定装置或者应该这样说工厂:

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 和你的方法之间的最佳方法是什么?

I fixed the fixture or should say the factory this way:

Factory.define :article do |e|
  e.name "Article001"
  e.user { |u| u.association(:user) }
end

that give me this new error...

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.

I just want to see my test the index method of my Article controller to pass. I that point I'm not interested by the user creation. And the error is telling me that a user has been created.

I tried your proposal but get stop with "stub"

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

What is the best approach between fixture:all and your method?

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