2011-01-12 7 views
1

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 

많은 감사 ...

답변

1

시도 CanCan을 버전 1.5로 업그레이드. 이전에 문제가 있었지만 업그레이드했을 때 문제가 해결되었다고 생각합니다.

관련 문제