ruby on rails - Why can't the post method in my Rspec test find the create action/route for my PostsController? -


hi have rspec test:

require "rails_helper"    describe postscontroller      let(:user){create(:user)}    describe "post #create"       "creates post"        expect {post :create, post: attributes_for(:post)}.to change(post, :count).by 1     end     end   end 

that throwing following error:

actioncontroller::urlgenerationerror:        no route matches {:action=>"create", :controller=>"posts", :post=>{:content=>"this post content!"}} 

which means can't find route right?

instead of passing :create first argument post have tried, passing route helper user_posts_path(user) <--- (this route posts create action), pretty same error. here attempt:

    "creates post"        expect {post user_posts_path(user), post: attributes_for(:post)}.to change(post, :count).by 1     end 

which throws error:

actioncontroller::urlgenerationerror:        no route matches {:action=>"/users/1/posts", :controller=>"posts", :post=>{:content=>"this post content!"}} 

i've attempted pass in id manually:

id: user.id 

as second argument post.

here post_factory.rb since calling attributes_for(:post):

factorygirl.define     factory :post      content "this post content!"     user   end end 

my relevant rake routes:

user_posts    /users/:user_id/posts(.:format)        posts#index                  post   /users/:user_id/posts(.:format)        posts#create    new_user_post    /users/:user_id/posts/new(.:format)    posts#new        edit_post    /posts/:id/edit(.:format)              posts#edit             post    /posts/:id(.:format)                   posts#show                  patch  /posts/:id(.:format)                   posts#update                  put    /posts/:id(.:format)                   posts#update                  delete /posts/:id(.:format)                   posts#destroy 

my postscontroller create action alive , too.

so know route there , have tried passing post method explicit route instead of :create, still same error. believe problem occurring because routes nested, need them nested, , test them in form can't change nesting. i'm not sure else do, i've come here help. thanks.

the posts#create action requires user_id passed part of url.

expect {post :create, post: attributes_for(:post)}.to change(post, :count).by 1 

should

expect {post :create, user_id: user.id, post: attributes_for(:post)}.to change(post, :count).by 1 

user available user factory lazily created(since used let , not let!) when call inside block.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -