2012-06-02 4 views
1

이 테스트를 통과하려고했지만 도움이 될 도움이 필요한 사람이 필요합니다. 지금은RSpec 기능 테스트가 잘못된 위치로 리디렉션됩니다.

describe 'PUT posts/:id' do 

    describe 'with valid attributes' do 

    let(:mock_post) { mock_model('Post', title: 'hey! iam a mock!', description: 'a sexy model', location: 'everywhere') } 

    login_user 

    it 'should update the object and redirect to the post' do 
     Post.stub!(:find).with(mock_post.id).and_return(mock_post) 

     Post.any_instance.should_receive(:update_attributes).with({"these" => "params"}).and_return(true) 

     response.should redirect_to post_path(mock_post) 

     put :update, id: mock_post.id, post: { these: 'params' } 
    end 

    it 'should have a current_user' do 
     subject.current_user.should_not be_nil 
    end 

    end 

, 위 테스트 및 다음과 같은 오류가 뭔가를 가지고 :

1) PostsController PUT posts/:id with valid attributes should update the object and redirect to the post 
    Failure/Error: response.should redirect_to post_path(mock_post) 
     Expected response to be a <:redirect>, but was <200> 
    # ./spec/controllers/posts_controller_spec.rb:200:in `block (4 levels) in <top (required)>' 

가 PostsController : 또한

class PostsController < ApplicationController 
    load_and_authorize_resource except: [:index, :show] 
    before_filter :authenticate_user!, except: [:index, :show, :tags] 
    before_filter :find_post, only: [:show, :edit, :update, :suspend, :suspend_alert] 

def update 
    if @post.update_attributes(params[:post]) 
    flash[:success] = 'Cool.' 
    redirect_to post_path(@post) 
    else 
    render :edit 
    end 
end 

protected 
    def find_post 
    @post = Post.find(params[:id]) 
    end 
end 

, 어떻게 내가 render :edit에 대한 테스트를 작성해야 부품?

+0

'login_user'가 갈해야'전 (각)' 블록; mock_post ID의 값을 하드 코드하지 마십시오. 그냥'1001'을'mock_post.id'로 대체하십시오. 'put' 테스트 액션을 기대 한대로 돌리십시오. 각 예제에 대해'put' 테스트 액션을 반복하십시오. – zetetic

+0

확인. 지침에 따라 소식을 업데이트했습니다. 'login_user'는'Devise' [wiki 페이지] (https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with)에서 복사 한'ControllerMacro'입니다. -Rails-3- (및 -rspec)). –

+0

'log/test.log'를 체크하면 리다이렉트가 일어나지 않는 이유를 알 수 있습니다. – zetetic

답변

2

사양에서는 컨트롤러 동작을 호출하지 않습니다. 추가하십시오 :

Post.any_instance. 
    should_receive(:update_attributes). 
    with({"these" => "params"}) 
put :update, :id => "1", :post => {"these" => "params"} 

update_attributes에 대한 호출 결과 두 개의 경로를 테스트하려면, 기대의 값을 대체 :

it "should redirect when successful" do 
    Post.any_instance. 
    should_receive(:update_attributes). 
    with({"these" => "params"}). 
    and_return(true)` 
    response.should_redirect_to(post_path(@mock_post)) 
    put :update, :id => "1", :post => {"these" => "params"} 
end 

it "should render the edit page when unsuccessful" do 
    Post.any_instance. 
    should_receive(:update_attributes). 
    with({"these" => "params"}). 
    and_return(false)` 
    response.should render_template("edit") 
    put :update, :id => "1", :post => {"these" => "params"} 
end 
+0

안녕하세요, 감사합니다. 그러나 어디에서 @ post를 얻었습니까? 나는 하나도 갖고 있지 않다. –

+0

죄송합니다. 내 편집을 참조하십시오. – zetetic

+0

내 게시물을 업데이트했습니다. 코드에 약간의 변경을가 했으므로 거의 효과가 있습니다. 이것을 봐주세요. 고맙습니다. –

관련 문제