2016-10-06 6 views
0

他の多くのユーザーが同じタイトルで問題を投稿しています。私はすべてのソリューションも試しました。しかし、すべてを試すにもかかわらず、私は 解決に達することができません。応答は<redirect>となる予定でしたが、Rspecのコントローラテストでは<200>でした

messages_controller_spec:

context 'with valid attributes' do 
    before :each do 
    user = FactoryGirl.create :user 
    sign_in user 
    end 
    it 'redirects to the root page' do 
    xhr :post, :create, messages: attributes_for(:message) 
    expect(response).to redirect_to root_path 
    end 
end 

今私MessageController:コンソールからRSpecの実行

def create 
    @message = current_user.messages.build(message_params) 
    @recipients = User.all 
    if @message.save 
    redirect_to root_path, notice: "Message Sent!" #The test fails here 
    else 
    flash[alert] = "Great Scott!" 
    render :new 
    end 
end 

は、次のエラーを与える:

Failure/Error: expect(response).to redirect_to root_path Expected response to be a <:redirect> but was <200>

私は、ログ/テストもチェックしています.rbファイル。リダイレクトは成功です。しかし応答はまだ200の代わりに300番台です:

[1m[36m (0.3ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (1.4ms)
[0m INSERT INTO "messages" ("body", "sender_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["body", "MyText"], ["sender_id", 1], ["created_at", "2016-10-06 11:16:14.934487"], ["updated_at", "2016-10-06 11:16:14.934487"]]
[1m[36m (0.3ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
Redirected to http://test.host/
Completed 200 OK in 40ms (ActiveRecord: 5.9ms) [1m[35m (0.6ms)[0m ROLLBACK

答えて

2

あなたはそれが

+0

感謝の男に続いて200

試してみるxhrを使用します!そんなばかげた間違い... –

+0

うん。 xhrがどのような状況で動作するのか、なぜそれがここではうまくいかないのか説明できますか? –

+0

@NovneetNovあなたのリクエストがajaxリクエストで、その場合には 'xhr'を使用します。しかし、ここではあなたのリクエストはアヤックスのリクエストではありません。それは単純な投稿要求であり、成功後にはroot_pathにリダイレクトされます。 –

関連する問題