RSpec 新手:Devise/Cancan 导致其他正常工作的控制器规范失败

发布于 2024-10-11 21:44:03 字数 1907 浏览 1 评论 0原文

我正在尝试让 RSpec 控制器规范通过。它与脚手架生成的规范几乎相同,只是用户首先登录到设备。 如果我从控制器禁用“load_and_authorize_resource”(检查权限),一切正常。但如果我把该行放回去,它就会失败:

  1) PostsController logged in administrator POST create with valid params assigns a newly created post as @post
     Failure/Error: post :create, :post => {'title' => 'test title'}
       <Post(id: integer, title: string, cached_slug: string, content: text, user_id: integer, created_at: datetime, updated_at: datetime) (class)> received :new with unexpected arguments
         expected: ({"title"=>"test title"})
              got: (no args)
     # ./spec/controllers/posts_controller_spec.rb:52:in `block (5 levels) in <top (required)>'

我假设规范没有正确登录用户,但 put current_user.role.name 确认用户已正确登录,并且具有必要的角色。在浏览器中执行实际过程确认其按预期工作。

有人有什么建议吗?我很困惑。下面的控制器:

  def create
    @post = Post.new(params[:post])
    @post.user = current_user
    respond_to do |format|
      if @post.save
        flash[:notice] = "Post successfully created"
        format.html { redirect_to(@post)}
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

...以及规范

  describe "with valid params" do
    it "assigns a newly created post as @post" do
      Post.stub(:new).with({'title' => 'test title'}) { mock_post(:save => true) }
      post :create, :post => {'title' => 'test title'}
      assigns(:post).should be(mock_post)
    end

...以及规范中的支持内容:

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

  def mock_post(stubs={})
    @mock_post ||= mock_model(Post, stubs).as_null_object
  end

非常感谢...

I'm trying to get an RSpec controller spec to pass. It's almost identical to the scaffold-generated spec, except a user is signed into devise first. If I disable 'load_and_authorize_resource' from the controller (which checks permissions), everything works fine. But if I put the line back in, it fails with:

  1) PostsController logged in administrator POST create with valid params assigns a newly created post as @post
     Failure/Error: post :create, :post => {'title' => 'test title'}
       <Post(id: integer, title: string, cached_slug: string, content: text, user_id: integer, created_at: datetime, updated_at: datetime) (class)> received :new with unexpected arguments
         expected: ({"title"=>"test title"})
              got: (no args)
     # ./spec/controllers/posts_controller_spec.rb:52:in `block (5 levels) in <top (required)>'

I had assumed the spec wasn't logging in the user correctly, but a puts current_user.role.name confirms the user is logged in correctly, and has the necessary role. Performing the actual process in a browser confirms it works as desired.

Anyone have any suggestions? I'm quite stumped. Controller below:

  def create
    @post = Post.new(params[:post])
    @post.user = current_user
    respond_to do |format|
      if @post.save
        flash[:notice] = "Post successfully created"
        format.html { redirect_to(@post)}
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

...And the spec

  describe "with valid params" do
    it "assigns a newly created post as @post" do
      Post.stub(:new).with({'title' => 'test title'}) { mock_post(:save => true) }
      post :create, :post => {'title' => 'test title'}
      assigns(:post).should be(mock_post)
    end

...And supporting stuff in the spec:

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

  def mock_post(stubs={})
    @mock_post ||= mock_model(Post, stubs).as_null_object
  end

Many thanks...

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

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

发布评论

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

评论(1

最美的太阳 2024-10-18 21:44:03

尝试将 CanCan 升级到版本 1.5。我早些时候遇到过这个问题,但我认为升级后它就消失了。

Try upgrading CanCan to version 1.5. I had the issue earlier but I think it went away when I upgraded.

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