RSpecコントローラの仕様を取得しようとしています。これは、ユーザがまず最初に覚えていることを除いて、足場生成の仕様とほぼ同じです。 コントローラから(許可をチェックする)「load_and_authorize_resource」を無効にすると、すべて正常に動作します。しかし、私は後ろにラインを入れた場合、それはで失敗します。RSpec Newbie:Devise/Cancanが動作していないコントローラの仕様を破損させる
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)>'
私はスペックが正しくユーザーにログインしていなかったと仮定していたが、プットの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
多くのおかげで...