I'm trying to send a 'delete' request with rspec. The problem is that I'm getting the following error :
undefined method `formats=' for Admin::CountriesController:Class
Here is my code :
describe 'Admin user deletion button' do
def sign_in(user)
user.confirm!
visit '/login'
fill_in 'user_email', with: user.email
fill_in 'user_password', with: user.password
click_button 'login_submit'
end
it 'should display an error' do
@controller = Admin::UsersController
@admin = FactoryGirl.create(:admin)
sign_in @admin
delete :delete, user_id: -1
end
end
EDIT : Here is the full error :
1) Admin user deletion button with existing user should display an error Failure/Error: delete :delete, user_id: -1 NoMethodError: undefined method 'formats=' for Admin::UsersController:Class # ./spec/controllers/admin_user_deletion_spec.rb:51:in `block (3 levels) in '
EDIT 2 : Here is the controller code :
module Admin
# Controller for users admin panel
class UsersController < AdminController
include UserHelper
before_action :actual_user
def users
@users = User.normals.page(params[:users_page])
@admins = User.admins.page(params[:admins_page])
end
def new
@user = User.new
render :new_user
end
def create
user = User.new(user_params)
if user.save
flash[:notice] = "L'utilisateur #{user.name} a bien été creé"
else
flash[:alert] = "L'utilisateur n'a pas pu être créé"
end
redirect_to action: :users
end
def edit
render :edit_user
end
def update
if @user.update(user_params) && @user.confirm!
flash[:notice] = "L'utilisateur #{@user.name} a bien été mis à jour"
else
flash[:alert] = "L'utilisateur #{@user.name} n'a pas pu être mis à jour"
end
redirect_to action: :users
end
def delete
name = @user.name
if @user.destroy
flash[:notice] = "L'utilisateur #{name} a bien été supprimé"
else
flash[:alert] = "L'utilisateur #{name} n'a pas pu être supprimé"
end
redirect_to action: :users
end
private
def user_params
params[:user].delete(:password) if params[:user][:password].blank?
params.require(:user).permit!
end
def actual_user
@user = User.find(params[:user_id]) if params[:user_id]
end
end
end
Controller specs make assumptions about their prerequisites--feature specs don't. You have confused the two, a common mistake.
You should never use visit in a controller spec. Instead, pass the request parameters and (if necessary) session data in the call to the action itself.
As to why your request fails, consider the error message:
NoMethodError: undefined method 'formats=' for Admin::UsersController:Class
This is because you have redefined the controller instance variable and assigned it the controller class, instead of a controller instance. Instead, leave the controller instance variable alone and allow RSpec to call the action on the instance.
If you need further convincing, check the return value of @controller.respond_to?(:formats=) within the context of a spec; in your version this returns false, but in a standard controller spec (e.g. one created by the RSpec generator), this will return true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With