与factory_girl、devise、rpsec 2.0 和rails 3.0 相关的问题=>无法获取 rspec 来验证控制器
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你的装置中有一个错误。错误消息表明它无法创建没有 user_id 的文章。
您可以修复固定装置,或者通过删除
fixtures :all
并存根 find 方法来避免使用它们:这告诉
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: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.我修复了固定装置或者应该这样说工厂:
这给了我这个新错误...
我只想看到我的文章控制器的索引方法的测试是否通过。我认为我对用户创建不感兴趣。该错误告诉我已经创建了一个用户。
我尝试了你的建议,但以“存根”停止了
fixture:all
和你的方法之间的最佳方法是什么?I fixed the fixture or should say the factory this way:
that give me this new error...
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"
What is the best approach between
fixture:all
and your method?