1

RSpecコントローラーの仕様を通過させようとしています。これは、ユーザーが最初にデバイスにサインインすることを除いて、scaffoldで生成された仕様とほぼ同じです。コントローラ(権限をチェックする)から「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)>'

仕様がユーザーに正しくログインしていないと想定していましたが、puts 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

どうもありがとう...

4

1 に答える 1

1

CanCan をバージョン 1.5 にアップグレードしてみてください。以前は問題がありましたが、アップグレードすると解消されたと思います。

于 2011-01-17T12:06:17.627 に答える