###前提・実現したいこと
RoRを用いて簡易的なブログアアプリケーションを作っています。
新規記事を投稿したあとにその投稿ページに遷移する、という機能を実装し、動作はしているのですがテストが通らず困っております。
RSpecでの遷移テストの書き方が非常に曖昧で困っております。
以下がコケているテストとそのメッセージになります。
###発生している問題・エラーメッセージ
Failure/Error: expect(response).to redirect_to blogs_path(assigns([:blogs])) Expected response to be a redirect to <http://test.host/blogs> but was a redirect to <http://test.host/blogs/96>. Expected "http://test.host/blogs" to be === "http://test.host/blogs/96".
###該当のソースコード
blogs_controller_spec.rb
ruby
1require 'rails_helper' 2 3RSpec.describe BlogsController do 4 describe 'POST #create' do 5 it 'save new post' do 6 expect do 7 post :create, blog: attributes_for(:blog) 8 end.to change(Blog, :count).by(1) 9 end 10 it "redirect to index" do 11 post :create, blog: attributes_for(:blog) 12 expect(response).to redirect_to blogs_path(assigns([:blogs])) 13 end 14 end 15end
blogs_controller.rb
class BlogsController < ApplicationController def new @blog = Blog.new end def create @blog = Blog.new(blog_params) if @blog.save redirect_to blog_path(@blog) else render :new end end private def blog_params params.require(:blog).permit(:title, :body) end end
###試したこと
blogs_path(assigns([:blogs]))に変えるなどはしましたが、変化なし。
###補足情報(言語/FW/ツール等のバージョンなど)
ruby 2.2.3
RoR 4.2.4
rspec 3.4.0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。