how to impements gem shoulda in unit testing rails 4 without rspec? - ruby-on-rails

how to implements gem shoulda in unit testing without respec?, this is my gemfile file :
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
group :production do
# Use PostgreSQL as the database for Active Record on Heroku
gem 'pg'
end
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem "minitest"
gem 'byebug'
end
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
# gem 'byebug', group: [:development, :test]
gem 'devise'
gem 'shoulda'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
and this is my test_helper.rb file :
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
include Devise::TestHelpers
require 'minitest/autorun'
require 'shoulda/rails'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
this is my example model testing unit :
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should validate_presence_of(:name)
should validate_presence_of(:last_name)
should validate_presence_of(:id_number)
should ensure_inclusion_of(:id_issuing_country).in_array(["Spain"])
should validate_presence_of(:birth_date)
should validate_presence_of(:email)
should validate_presence_of(:bank_account)
end
when i runing this in my console : bin/rake test test/models/user_test.rb
i get error like this :
LoadError: cannot load such file -- shoulda/rails

From a gem file for application I am working on
....
group :test do
# gem 'faker'
gem 'launchy'
gem 'relish'
gem 'selenium-webdriver', '~> 2.42.0'
gem 'capybara'
gem 'database_cleaner'
gem 'shoulda-matchers', '~>2.6.2'
gem 'shoulda'
end
....
hope this helps - Pierre

Related

Why am I getting unititialized constant in my Rspec model test?

I'm trying to create a test with Rspec in my rails application but I keep getting the following error.
uninitialized constant Build
# ./spec/models/build_spec.rb:3:in `<top (required)>'
No examples found.
I am running rails version 6.1.4.1. I also know that my model exists as I can create new objects that belong to that table in the ruby console.
here is my rspec file (spec/models/build_spec.rb)
require 'spec_helper'
RSpec.describe Build, type: :model do
it "Has a name"
it "is not valid without name"
it "is not valid with too short name"
it "is not valid with too long of a name"
end
here is my gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.4'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.4', '>= 6.1.4.1'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
gem 'will_paginate', '~> 3.3'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4.2'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 4.1.0'
# Display performance information such as SQL time and flame graphs for each request in your browser.
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
gem 'rack-mini-profiler', '~> 2.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 3.26'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
gem 'rspec-rails', '>= 3.9.0'
end
group :production do
gem 'pg', '~> 1.1'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
spec_helper does not load the environment (this includes not loading the models). You could either require the model file explicitly in this spec or require rails_helper instead.
Check you have the right model inside the app/models/ directory.
spec/models/build_spec.rb
require 'rails_helper'
RSpec.describe Build, type: :model do
it { expect(described_class.new).to be_a() }
end
app/models/build.rb
class Build < ApplicationRecord
end
Hope this works for you.

Rails Print Cups

In my rails app I am priting receipt using cups with gem cupsffi
This is the job I have set:
class PrintReceiptJob < ActiveJob::Base
require 'cupsffi'
printers = CupsPrinter.get_all_printer_names
printer = CupsPrinter.new(printers.first)
job = printer.print_file(Rails.root.join('receipt.pdf').to_s, {'PageSize' => 'A4'})
end
In my controller I generate the receipt in pdf with prawn and call the job above:
pdf = ReceiptPdf.new(#receipt)
pdf.render_file "receipt.pdf"
PrintReceiptJob.perform_now
And it works, the receipt gets printed.
Problem is, after printing I get this error:
[ActiveJob] [PrintReceiptJob] [044c6b97-daca-445d-9c87-631e3de3d7cd] Performing PrintReceiptJob (Job ID: 044c6b97-daca-445d-9c87-631e3de3d7cd) from Async(default)
[ActiveJob] [PrintReceiptJob] [044c6b97-daca-445d-9c87-631e3de3d7cd] Error performing PrintReceiptJob (Job ID: 044c6b97-daca-445d-9c87-631e3de3d7cd) from Async(default) in 0.52ms: NotImplementedError (NotImplementedError):
For what I understand it's trying to perform the job twice, and second time it fails.
application.rb
require_relative 'boot'
require 'apartment/elevators/subdomain'
require 'rails/all'
require File.expand_path('../boot', __FILE__)
ENV['RANSACK_FORM_BUILDER'] = '::SimpleForm::FormBuilder'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CompanyManagement
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
config.time_zone = "Singapore"
end
end
Gemfile:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'mini_racer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
#gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'devise'
gem 'bootstrap'
gem 'jquery-ui-rails'
gem 'jquery-rails'
gem 'simple_form'
gem "font-awesome-rails"
gem "cocoon"
gem "select2-rails"
gem 'will_paginate'
gem 'will_paginate-bootstrap4'
gem "chartkick"
gem 'groupdate'
gem 'trix-rails', require: 'trix'
gem 'amoeba'
gem "simple_calendar", "~> 2.0"
gem 'apartment'
gem 'protokoll'
gem 'ransack'
gem "time_splitter"
gem 'working_hours'
gem 'jquery-minicolors-rails'
gem 'carrierwave', '~> 1.0'
gem 'prawn-rails'
gem 'cupsffi'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'pry'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15', '< 4.0'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
What am I missing? How can I fix?
You forgot to wrap your code with method:
require 'cupsffi'
class PrintReceiptJob < ActiveJob::Base
def perform
printers = CupsPrinter.get_all_printer_names
printer = CupsPrinter.new(printers.first)
job = printer.print_file(Rails.root.join('receipt.pdf').to_s, {'PageSize' => 'A4'})
end
end

rake aborted! cannot load such file -- ap

I have build my new rails application and when I am trying to deploy to Apache server while executing bundle exec rake secret I am getting rake aborted! cannot load such file -- ap.This is the URL I have referred to deploy Deploying a Ruby app on a Linux/Unix production server.
Please look into my Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use sqlite3 as the database for Active Record
gem 'mysql2'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'jquery-rails'
gem 'json'
gem 'jenkins_api_client'
gem 'mailfactory'
gem 'docx','>=0.2.07'
# gem 'jquery-turbolinks'
gem 'net-scp'
gem 'rubyXL'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'bcrypt'
# font awsome
gem "font-awesome-rails"
# parsley validation
gem "parsley-rails"
# devise for athentication
gem 'devise'
# gem "devise_ldap_authenticatable"
gem "toastr-rails"
# datapciker
gem 'bootstrap-datepicker-rails'
# image uploader
gem 'carrierwave'
# Active admin for admin
# gem 'activeadmin'
# To set dynamic value in docx
gem 'sablon'
# State_machine for status
gem 'aasm'
# Soft delete
gem 'paranoia'
# for seed
gem 'seed-fu'
# Net-SSH gem to exec batch file.
gem 'net-ssh'
# Schedule job
gem 'whenever', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'pry'
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem "awesome_print", require:"ap"
# gem 'quiet_assets'
gem 'pry-rails'
gem 'bullet'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
This is my first application i tried to deploy, please help to resolve this issue
My ruby version is
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux]
rails version - 5.1.4
This line in the Gemfile seems strange to me
gem "awesome_print", require:"ap"
Can you remove the require: "ap" and check if it works then?

Twitter-bootstrap-rails: does it install Bootstrap into a project?

I do everything according to Readme but having an issue when starting a rails app: Sprockets::FileNotFound And it points to //= require twitter/bootstrap Does this gem really install bootstrap stylesheets and corresponding js libraries or one is supposed to install them manually (with bower or in another way)?
Here is my Gemfile:
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5.1'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :assets do
gem 'twitter-bootstrap-rails'
end
gem 'responders', '~> 2.1.1'
gem 'haml'
group :development, :test do
gem 'pry-byebug'
gem 'pry-rails'
gem 'rspec-expectations', '~> 3.0.0'
gem 'spring-commands-rspec'
#gem 'guard-rspec', require: false
gem 'rspec-rails', '~> 3.0.0'
# Support for its syntax
gem 'rspec-its', '~> 1.0.1'
gem 'shoulda-matchers', '~> 3.1.1'
gem 'factory_girl_rails'
gem 'ffaker'
gem "bower-rails", "~> 0.10.0"
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
gem 'haml-rails'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
I'd say that because group assets were dropped in Rails 4. From the docs:
Rails 4.0 removed the assets group from Gemfile. You'd need to remove that line from your Gemfile when upgrading. You should also update your application file (in config/application.rb):
So instead of using it
....
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :assets do
gem 'twitter-bootstrap-rails'
end
....
Just remove the group block:
....
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'twitter-bootstrap-rails'
....
And then run bundle install and restart the server.
More info - Why did Rails4 drop support for “assets” group in the Gemfile

rspec using old files when rerunning tests

This is my first project in Rails 4, I'm using 4.2
I'm finding that rspec is not reloading my spec files each time it runs, so tests that I've fixed continue to fail with the old error message.
I'm guessing this has something to do with spring but I can't seem to find information on how to configure paths it should reload.
I'm launching rspec using bin/rspec, and here's my Gemfile if that's of use.
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Angular CSRF handling
gem 'angular_rails_csrf'
gem 'pg'
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
#gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
gem 'sass', '3.2.19'
group :test, :development do
gem "factory_girl_rails", "~> 4.0"
gem "capybara"
gem "database_cleaner"
gem "selenium-webdriver"
gem "teaspoon-jasmine"
gem 'teaspoon'
gem 'phantomjs'
gem 'guard-teaspoon'
gem 'spring-commands-teaspoon'
gem 'rspec-rails'
gem 'spring-commands-rspec'
gem 'guard-rspec'
gem 'rb-readline'
gem 'rb-fsevent' if `uname` =~ /Darwin/
end
gem 'bootstrap-sass', '~> 3.1.1'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bson_ext', '~> 1.8.6'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', '1.3.6'
end
# Preload angular templates
gem 'angular-rails-templates'

Resources