I am testing my Ruby on rails application using RSpec, FactoryBot and shoulda gems. I've tested my models and it worked perfectly but when I tried to test the controller it shows this error. I also checked my batches_controller.rb file.
batches_controller.rb
class BatchesController < ApplicationController
before_action :authenticate_user!
def index
@batches = Batch.all
end
def new
@batch = Batch.new
def show
@batch = Batch.find(params[:id])
end
end
spec/support/devise.rb
require_relative './controller_macros'
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
end
spec/support/controller_macros.rb
module ControllerMacros
def login_admin
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryBot.create(:admin)
end
end
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
sign_in FactoryBot.create(:user)
end
end
end
spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
require 'spec_helper'
require 'rspec/rails'
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'factory_bot_rails'
require 'devise'
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryBot::Syntax::Methods
config.use_transactional_fixtures = true
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.include Devise::Test::ControllerHelpers, :type => :controller
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
spec/controllers/batches_controller_spec.rb
require 'rails_helper'
require_relative "../support/devise"
RSpec.describe BatchesController, type: :controller do
describe "user signed in or not" do
login_user
context "first test" do
it " have a current_user" do
expect(subject.current_user).to_not eq(nil)
end
end
end
end
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.1'
gem 'rails', '~> 6.0.3', '>= 6.0.3.1'
gem 'devise'
gem 'activeadmin'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 4.1'
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.7'
gem 'bootsnap', '>= 1.4.2', require: false
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails', ">= 3.9.0"
gem 'shoulda-matchers'
gem 'factory_bot_rails'
end
group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
gem 'webdrivers'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
What am I doing wrong here?
Aucun commentaire:
Enregistrer un commentaire