Testing comments controller. Rspec - ruby-on-rails

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:inblock (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 '

I think your main problem is that you send to action already created object
post :create, comment: Comment.create(comment_attr)
Rails parses params and fetches id of record and trying to find member route.
It looks like you would like to test that a new Comment will be created from params in controller. And it is nested resource so you should send hotel_id as well
Try to send
it 'creates a new comment with valid attributes' do
comment_attr = attributes_for(:comment)
hotel = Hotel.last || create(:hotel)
expect{
post :create, comment: comment_attr, hotel_id: hotel.id
}.to change(Comment,:count).by(1)
end
Another thing which confuses me is line post 'comment', only: :create in routes. I have never seen option only for post - usually it is used for resources.

Related

in rspec controller create post with current_user

Models:
class User < ApplicationRecord
has_many :posts
end
class Post < ApplicationRecord
belongs_to :user
end
Controllers:
class PostsController < ApplicationController
def create
#post = current_user.posts.creata(post_params)
end
def post_params
params.require(:post).permit(:name,:user_id)
end
end
RSpec.describe PostsController do
describe "create post" do
it "should create"
user = create(:user)
expect{
post :create, params: { name: "new post", user_id: user.id }
}.to change(Post,:count).by(1)
end
end
And this error:
ActionController::ParameterMissing:
param is missing or the value is empty: post
On rails 5 you need pass the whole params structure. See below:
Rails 4:
post :create, post: { name: "new post" }
Rails 5:
post :create, params: { post: { name: "new post" } }
Your specs is also missing the current_user stub:
RSpec.describe PostsController do
describe "create post" do
before do
user = create(:user)
allow_any_instance_of(PostsController).to receive(:current_user) { user }
end
it "should create"
expect {
post :create, params: { post: { name: "new post" } }
}.to change(Post, :count).by(1)
end
end

Why does my rspec test fail?

I have problem with my "POST create" action. Test passed successfully, when attributes are valid, but when they are invalid, player is also saved. It's strange because, only :invalid_player, can be saved with invalid attributes. If I change for example, wins to -1 or "string", player with attributes :invalid_player is saved. But if I change attributes for :player, like wins = -1, validators prevent player, from being saved.
Console output with error message:
Failures:
1) PlayersController user is signed in POST create with invalid attributes does not save the new player
Failure/Error:
expect{
post :create, { tournament_id: #tournament, player: FactoryGirl.attributes_for(:invalid_player) }
}.to_not change(Player, :count)
expected #count not to have changed, but did change from 1 to 2
# ./spec/controllers/players_controller_spec.rb:111:in `block (5 levels) in <top (required)>'
This is my Player model:
class Player < ActiveRecord::Base
belongs_to :user
belongs_to :tournament
validates :wins, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :loses, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :draws, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
end
Factory file for players:
FactoryGirl.define do
factory :player do
wins 0
loses 0
draws 0
end
factory :invalid_player, parent: :player do
wins -1
loses 0
draws 0
end
end
Spec test:
context "user is signed in" do
before do
#tournament = create(:tournament)
#player = create(:player)
#user = create(:user)
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in(#user)
controller.stub(:current_user).and_return(#user)
end
describe "GET new" do
end
describe "GET index" do
it "renders the :index view" do
get :index, tournament_id: #tournament
expect(response).to render_template :index
end
end
describe "GET show" do
it "renders the :show view" do
get :show, { id: #player, tournament_id: #tournament }
expect(response).to render_template :show
end
end
describe "POST create" do
context "with valid attributes" do
it "creates a new player" do
expect{
post :create, { tournament_id: #tournament, player: FactoryGirl.attributes_for(:player) }
}.to change(Player,:count).by(1)
end
it "redirects to the tournament" do
post :create, { tournament_id: #tournament, player: FactoryGirl.attributes_for(:player) }
expect(response).to redirect_to #tournament
end
end
context "with invalid attributes" do
it "does not save the new player" do
expect{
post :create, { tournament_id: #tournament, player: FactoryGirl.attributes_for(:invalid_player) }
}.to_not change(Player, :count)
end
it 're-renders the new method' do
post :create, { tournament_id: #tournament, player: FactoryGirl.attributes_for(:invalid_player) }
response.should render_template :new
end
end
end
end
Controller:
class PlayersController < ApplicationController
before_action :set_tournament
before_action :set_admin, only: [:edit, :update, :destroy]
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
def index
#players = #tournament.players.all
end
def show
#player = Player.find(params[:id])
end
def new
#player = #tournament.players.new
end
def create
if current_user.player.nil? == false
flash[:error] = "You're already in tournament."
redirect_to tournaments_url
else
#player = #tournament.players.new
#player.user_id = current_user.id
if #player.save
redirect_to #tournament
else
render 'new'
end
end
end
def edit
if current_user == #admin
#player = #tournament.players.find(params[:id])
else
redirect_to tournaments_url
end
end
def update
if current_user == #admin
#player = #tournament.players.find(params[:id])
if #player.update_attributes(game_params)
flash[:success] = "Player was updated successful"
redirect_to #tournament
end
else
redirect_to tournaments_url
end
end
def destroy
#player = Player.find(params[:id])
flash[:success] = "Player deleted"
redirect_to #tournament
end
private
def set_tournament
#tournament = Tournament.find(params[:tournament_id])
end
def set_admin
#tournament = Tournament.find(params[:tournament_id])
#admin = #tournament.user
end
end
You are not assigning any attributes to your model in your create method. You need to do the following (I assume it's rails 4):
#player = #tournament.players.new(player_params)
#...
private
def player_params
params.require(:player).permit(:wins, :loses, :draws)
end
Without any assignment you most likely falling back onto database default value of zero, which is valid.

expecting <"index"> but render with <[]>

I'm having problems to pass a test in rails with rspec. This is what console tells me when I ran the tests.
The fail is ControlsController GET index logged in renders the index template
Failure/Error: expect(response). to render_template(:index)
expecting <"index"> but rendering with <[]>
And this is my code
require "rails_helper"
RSpec.describe ControlsController, :type => :controller do
render_views
describe "GET index" do
let(:user) {
FactoryGirl.create(:user)
}
let(:control) {
FactoryGirl.create(:control, user: user)
}
context "logged in" do
before :each do
sign_in :user, user
end
it "loads all controls into #controls" do
get :index, { user_id: user.id}
expect(assigns(:controls)).to eq([control])
end
it "assigns a new control to #control" do
get :index, { user_id: user.id}
expect(assigns(:control)).to be_a_new(Control)
end
it "renders the index template" do
get :index, { user_id: user.id}
expect(response). to render_template(:index)
end
it "a user can't see the controls from other user" do
new_user = User.create(name: "Juan",
email: "juan#gmail.com",
password: "123456789",
password_confirmation: "123456789")
get :index, { user_id: new_user.id}
expect(response).to redirect_to root_path
end
class ControlsController < ApplicationController
before_action :authenticate_user!
def index
#user= current_user
#control= Control.new
# #control_last = Control.lastcontrol (current_user.id)
# #controls_average = Control.controls_average (current_user.id)
# #controls_average_day = Control.controls_day_average (current_user.id)
#controls = Control.all
if params[:user_id] != current_user.id
redirect_to root_path
end
end
The answer is to make a private method and redirect_to user_controls_path current_user.name
This is the new code of the controller
controlsController.rb
class ControlsController < ApplicationController
before_action :authenticate_user!
before_action :redirect_if_not_current_user, only: :index
private
def control_params
params.require(:control).permit(:level, :period, :day)
end
def redirect_if_not_current_user
if params[:user_id] != current_user.name
redirect_to user_controls_path current_user.name
end
end

Rspec controller test not passing validations, but has valid data

I'm trying to write a controller test for my update route using Rspec with factory girl but I can't get my test to pass validations even though I'm pretty sure the data in my factories is valid.
Here are my factories:
FactoryGirl.define do
factory :user do
username { Faker::Internet.user_name(8) }
password 'password'
end
factory :post do
title { Faker::Lorem.sentence }
body { Faker::Lorem.paragraph }
author { Faker::Internet.user_name(8) }
end
end
here is my model:
class Post < ActiveRecord::Base
validates :title, :body, :author, presence: true
end
Here is my test:
require 'rails_helper'
describe PostsController do
let!(:user) { FactoryGirl.create :user }
let!(:post) { FactoryGirl.create :post }
let(:attributes) { FactoryGirl.attributes_for :post }
describe 'PUT #update' do
let(:title) { "A treatise on Malomars." }
it 'updates a field on a blog post' do
put :update, id: post.id, post: {title: title}
expect(post.reload.title).to eq(post.title)
end
end
end
The error I'm getting is:
Failure/Error: put :update, id: post.id, post: {title: title}
ActiveRecord::RecordInvalid:
Validation failed: Body can't be blank
EDIT---
here is the controller:
class PostsController < ApplicationController
def index
#posts = Post.all
end
def new
end
def create
post = Post.new
post.title = params[:title]
post.body = params[:body]
post.author = "#{session[:username].titleize} Force"
redirect_to root_path
post.save!
end
def show
p session[:id]
#post = Post.find(params[:id])
end
def update
post = Post.find(params[:id])
post.title = params[:post][:title]
post.body = params[:post][:body]
post.save!
redirect_to root_path
end
def destroy
post = Post.find(params[:id])
post.destroy
redirect_to root_path
end
end
Strong parameters aside, you're setting post.body to nil since you're not passing a value for the body parameter in your test. When you call save! in your controller, you're getting an error because you have a validation for body being present (i.e. not nil).

FactoryGirl doesnt save records to the db

I test my destroy and update methods in hotel_controller and Im keep getting ActiveRecord:RecordNotFound error. Heres a screenshot
I think this is coz FactoryGirs doesnt save recods to the db. Help me pls to get things right.
hotels_controller.rb
class HotelsController < ApplicationController
before_action :signed_in_user, except: [:index, :show, :top5hotels]
...
def destroy
#hotel = current_user.hotels.find(params[:id])
#hotel.destroy
redirect_to hotels_url
end
def update
#hotel = current_user.hotels.find(params[:id])
if #hotel.update_attributes!(params[:hotel])
redirect_to #hotel, notice: "Hotel was successfully updated."
else
render "edit"
end
end
...
end
factories.rb
FactoryGirl.define do
factory :hotel do
name 'NewHotel'
star_rating 5
breakfast false
room_description 'Room Description'
price_for_room 500
user { create(:user) }
address { create(:address) }
end
factory :user do
sequence(:email) { |n| "user_mail.#{n}#gmail.com" }
name 'Yuri Gagarin'
password 'foobar'
password_confirmation 'foobar'
end
factory :rating do
value 5
user { create(:user) }
hotel { create(:hotel) }
end
factory :comment do
body "Heresanytextyouwant"
user { create(:user) }
hotel { create(:hotel) }
end
factory :address do
country 'Country'
state 'State'
city 'City'
street 'Street'
end
end
hotels_controller_spec.rb
require 'spec_helper'
describe HotelsController do
before { sign_in user, no_capybara: true }
...
describe "destroy action" do
it "redirects to index action when hotel is destroyed" do
hotel = create(:hotel)
delete :destroy, id: hotel.id
expect(response).to redirect_to(hotels_url)
end
end
describe "update action" do
it "redirects to the hotel" do
hotel = create(:hotel)
put :update, id: hotel.id, hotel: FactoryGirl.attributes_for(:hotel)
expect(assigns(:hotel)).to be_eq(hotel)
#expect(response).to render_template('show')
end
end
end
FactoryGirl IS saving records to db.
The trouble is the current_user is not the same user that the hotel belongs to, so when you try to retrieve the hotel record it's not found.
Try changing...
#hotel = current_user.hotels.find(params[:id])
to...
#hotel = Hotel.find(params[:id])
And you'll see it works.
If you want to keep the original code, then in the test you should be doing...
hotel = create(:hotel, user: current_user)

Resources