I want to test comments controller, action create, but I don't know how do this. comments_controller
class CommentsController < ApplicationController
def create
@hotel = Hotel.find(params[:hotel_id])
@comment = @hotel.comments.new(comment_params)
@comment.user_id = current_user.id
@comment.save
redirect_to @hotel
end
private
def comment_params
params.require(:comment).permit(:user_id, :body, :hotel_id)
end
end
routes.rb
resources :hotels do
resources :comments
get 'list', on: :collection
post 'comment'
end
comments_controller_spec.rb
require 'rails_helper'
describe CommentsController do
login_user
describe 'POST create' do
it 'create a new comment with valid attributes' do
expect{
comment_attr = attributes_for(:comment)
post :create, comment: comment_attr
}.to change(Comment,:count).by(1)
end
it 'redirects to the new comment' do
comment_attr = attributes_for(:comment)
post :create, comment: comment_attr
expect(response).to redirect_to hotels_path(hotel)
end
end
end
factories.rb
FactoryGirl.define do
factory :user do |user|
user.email { Faker::Internet.email }
user.password 'password'
user.password_confirmation 'password'
end
factory :hotel do |hotel|
hotel.title 'Hotel'
hotel.description 'This is a some description for hotel'
hotel.breakfast true
hotel.price 20500
hotel.address { create(:address) }
hotel.user { create(:user) }
hotel.avatar { fixture_file_upload(Rails.root + 'spec/fixtures/images/example.jpg', "image/jpg") }
end
factory :comment do |comment|
comment.body 'This is a some comment ^_^'
comment.user { create(:user) }
comment.hotel { create(:hotel) }
end
factory :address do |address|
address.country { Faker::Address.country }
address.state { Faker::Address.state }
address.city { Faker::Address.city }
address.street { Faker::Address.street_name }
end
end
But I have this error:
1) CommentsController POST create create a new comment with valid attributes Failure/Error: post :create, comment: comment_attr ActionController::UrlGenerationError: No route matches {:comment=>{:body=>"This is a some comment ^_^", :user=>"5", :hotel=>"1"}, :controller=>"comments", :action=>"create"} # ./spec/controllers/comments_controller_spec.rb:10:in
block (4 levels) in <top (required)>' # ./spec/controllers/comments_controller_spec.rb:8:in
block (3 levels) in '2) CommentsController POST create redirects to the new comment Failure/Error: post :create, comment: comment_attr ActionController::UrlGenerationError: No route matches {:comment=>{:body=>"This is a some comment ^_^", :user=>"5", :hotel=>"1"}, :controller=>"comments", :action=>"create"} # ./spec/controllers/comments_controller_spec.rb:16:in `block (3 levels) in '