2012-06-02 38 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的值。只需調用它,即用'mock_post.id'替換'1001'; 使「放入」測試行爲符合預期。重複每個示例的「put」測試操作。 – zetetic

+0

好的。按照您的指示更新了帖子。 'login_user'是我從'Devise' [wiki頁面]複製的'ControllerMacro'(https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with -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

我已更新我的帖子。我對代碼做了一些更改,所以它幾乎可以工作。請看一下。謝謝。 –