Ruby 过滤器和 RSpec 问题

发布于 2024-12-23 02:06:32 字数 1521 浏览 1 评论 0原文

我不明白为什么这个 RSpec 测试失败。有什么建议吗?
我尝试处理帖子的破坏。只有创建帖子的用户才能删除它们。

class PostsController < ApplicationController
  before_filter :authorized_user, :only => :destroy

def destroy
  post = Post.find(params[:id])
  post.destroy
  redirect_to posts_path, notice: 'Post successfully destroyed'
end

private  
 def authorized_user
  redirect_to posts_path, notice: 'Access Denied' if current_user.posts.find_by_id(params[:id]).nil?
end

测试:

describe "DELETE destroy" do
 before(:each) do
   @post = stub_model(Post, :id => 23)
   @post.stub(:destroy){true}
   Post.stub(:find){@post}
 end
 it "should search the post" do
  Post.should_receive(:find).with(@post.id.to_s).and_return(@post)
  delete :destroy, {:id => @post.id }
 end

 it "should destroy the post" do
  @post.should_receive(:destroy)
  delete :destroy, {:id => @post.id }
 end
 it "should redirect to the posts list" do
  delete :destroy, {:id => @post.id }
  response.should redirect_to posts_path
 end
end

和错误:

  1) PostsController DELETE destroy should search the post
 Failure/Error: Post.should_receive(:find).with(@post.id.to_s).and_return(@post)
   (<Post(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime, user_id: integer) (class)>).find("23")
       expected: 1 time
       received: 0 times
 # ./spec/controllers/posts_controller_spec.rb:67:in `block (3 levels) in <top (required)>'

I can't figure out why this RSpec test fails. Any advice?
I try to handle the destruction of post. Only users who create posts can delete them.

class PostsController < ApplicationController
  before_filter :authorized_user, :only => :destroy

def destroy
  post = Post.find(params[:id])
  post.destroy
  redirect_to posts_path, notice: 'Post successfully destroyed'
end

private  
 def authorized_user
  redirect_to posts_path, notice: 'Access Denied' if current_user.posts.find_by_id(params[:id]).nil?
end

Test :

describe "DELETE destroy" do
 before(:each) do
   @post = stub_model(Post, :id => 23)
   @post.stub(:destroy){true}
   Post.stub(:find){@post}
 end
 it "should search the post" do
  Post.should_receive(:find).with(@post.id.to_s).and_return(@post)
  delete :destroy, {:id => @post.id }
 end

 it "should destroy the post" do
  @post.should_receive(:destroy)
  delete :destroy, {:id => @post.id }
 end
 it "should redirect to the posts list" do
  delete :destroy, {:id => @post.id }
  response.should redirect_to posts_path
 end
end

And errors :

  1) PostsController DELETE destroy should search the post
 Failure/Error: Post.should_receive(:find).with(@post.id.to_s).and_return(@post)
   (<Post(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime, user_id: integer) (class)>).find("23")
       expected: 1 time
       received: 0 times
 # ./spec/controllers/posts_controller_spec.rb:67:in `block (3 levels) in <top (required)>'

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

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

发布评论

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

评论(1

爱格式化 2024-12-30 02:06:32

我认为这是因为在 before(:each) 块中您必须登录用户。你有 before_filter 并且这个过滤器仅用于 :destroy。因此,当您没有登录用户时,您的测试将失败,因为它实际上是没有“运行测试”权限的用户。因此,您应该将此代码放在 before(:each):

user = way_to_make_user - I do it with: FactoryGirl.create(:user,role=>"client")
controller.sign_in user

如果您告诉我您使用什么进行身份验证,我可以为您提供更多帮助。我用康康宝石。所以要点是每次测试之前都必须登录用户。

I think it is because in the before(:each) block you have to log in a user. You have before_filter and this filter is only for :destroy. So when you don't have a logged in user then your tests will fail because it is actually a user without permissions that "runs the test". So you should put this code in before(:each):

user = way_to_make_user - I do it with: FactoryGirl.create(:user,role=>"client")
controller.sign_in user

I could help you more if you tell me what do you use for authentication. I use cancan gem. So the main point is that you have to log in user before each test.

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