How to I switch my sinatra server between development and testing? - testing

I have a sinatra server, and according to the gemfile, there are different groups of gems. Specifically, there is an application group, a testing group, and a development group. How do I switch my application between these three groups? Specifically, I want to be running my server in testing mode so I can do TDD.
Here is the gemfile.
source 'https://rubygems.org'
ruby '2.0.0'
gem 'sinatra'
gem 'sinatra-contrib', require: %w(sinatra/config_file)
gem 'thin'
gem 'slim'
gem 'sinatra-assetpack'
gem 'zurb-foundation'
gem 'compass'
group :application do
# Here should be all the specifics for the application
end
group :test do
gem 'rspec'
gem 'coveralls'
gem 'capybara'
gem 'rubocop'
end
group :development do
gem 'rake'
gem 'guard', '2.2.3'
gem 'guard-rspec'
gem 'guard-rubocop'
gem 'guard-livereload'
gem 'guard-shotgun', git: 'git#github.com:rchampourlier/guard-shotgun.git', branch: 'master'
gem 'blam'
end

Gem Groups
Bundler installs by default everything. What you probably want is something like this:
$ bundle install --without test development
for your productive server, this install everything except the test & development group.
source: http://bundler.io/v1.3/groups.html
Sinatra Environment
Since you mentions sinatra explicit you maybe also interested in the sinatra config blocks
# run only in development environment
configure :development do
use Rack::Session::Pool, :key => '', :expire_after => 60 * 10
end
# run only in test environment
configure :test do
use Rack::Session::Pool, :key => 'session', :expire_after => 60 * 60
end
# run everytime
configure do
MongoMapper.database = 'food'
User.ensure_index(:username)
Product.ensure_index(:rnd)
end
You can set the different environments with RACK_ENV.
example:
RACK_ENV=test ruby mytest.rb
source: http://www.sinatrarb.com/configuration.html#built-in-settings

Related

What is the correct way to fix bundler not finding compatible versions

I have an old rails 3 application which I want to update to rails 4 at the moment. I removed the Gemfile.lock manually and changed the Gemfile to require rails in version "4.0.0" before running a fresh "bundle install". I was getting several messages that bundler was not able to find compatible versions.
I am using ruby 2.0.0 here installed with rvm on a linux system.
I installed rails 4.0.0 manually using
gem install rails -v 4.0.0
And then I tried again with
bundle install
This is one example of the messages that pop up:
Bundler could not find compatible versions for gem "activerecord":
In Gemfile:
activerecord-import (~> 0.4.1) was resolved to 0.4.1, which depends on
activerecord (>= 3.0)
authlogic (~> 3.3.0) was resolved to 3.3.0, which depends on
activerecord (>= 3.2)
delayed_job_active_record (~> 4.0.0) was resolved to 4.0.3, which depends on
activerecord (>= 3.0, < 5.0)
rails (= 4.0.0) was resolved to 4.0.0, which depends on
activerecord (= 4.0.0)
This is my Gemfile:
source 'https://rubygems.org'
# the rails framework: September 14, 2016
gem 'rails', '4.0.0'
# the postgres db connector
gem 'pg', '~> 0.18.0'
# a templating engine for html
gem 'haml', '~> 3.1.7'
# authentication solution
gem 'authlogic', '~> 3.3.0'
# role-based authorization system
gem 'acl9', '~> 0.12.1'
# helper methods for rails 3 models
gem 'dynamic_form', '~> 1.1.4'
# upload management for active record
gem 'paperclip', '~> 4.2.2'
# tag a single model on several contexts
gem 'acts-as-taggable-on', '~> 2.4.1'
# read and write spreadsheet documents
gem 'spreadsheet', '0.9.7'
# active record backend for Delayed::Job
gem 'delayed_job_active_record', '~> 4.0.0'
# wrap ruby scripts executed as deamon
gem 'daemons', '~> 1.1.9'
# writing and deploying cron jobs
gem 'whenever', '~> 0.8.2', require: false
# bulk inserting data using active record
gem 'activerecord-import', '~> 0.4.1'
# named scoples for postgres fulltext search
gem 'pg_search', '~> 0.5.7'
# jQuery and the jQuery-ujs driver for your Rails
gem 'jquery-rails', '~> 3.1.3'
# jQuery UI's JavaScript, CSS, and image files
gem 'jquery-ui-rails', '~> 3.0.1'
# paginated queries with Active Record
gem 'will_paginate', '~> 3.0.5'
# group :production do
# performance management system
# gem 'newrelic_rpm'
# end
# Profiling toolkit
# gem 'rack-mini-profiler', :group => :development
group :test, :development do
# unit testing framework
gem 'test-unit', '~> 2.5.4'
# process manager
gem 'foreman', '~> 0.61.0'
# code profiler
# gem 'ruby-prof'
# bindings for the GNOME Libxml2
gem 'libxml-ruby', '~> 2.4.0'
# wrapper for Linux inotify
gem 'rb-inotify', '~> 0.9.1'
# web server
gem 'thin', '~> 1.5.0'
# gem 'debugger'
gem 'better_errors', '~> 0.3.2'
# retrieve the binding of a method's caller
gem 'binding_of_caller', '~> 0.8.0'
# hosted test coverage service (badge)
gem 'coveralls', '~> 0.8.23', require: false
# gem 'codecov', :require => false, :group => :test
end
group :assets do
# sass adapter
gem 'sass-rails', '~> 3.2.3'
# coffee script adapter
gem 'coffee-rails', '~> 3.2.1'
# call java script code and manipulate java script
# gem 'therubyracer', :platforms => :ruby
# uglifier minifies java script
gem 'uglifier', '~> 2.7.2'
end
group :tools do
# code formatter
gem 'rubocop', require: false
# report vulnerable gems
gem 'gemsurance', '~> 0.9.0'
# handle events on file system modifications
gem 'guard', '~> 1.6.1'
# automatically run your tests on file modification
gem 'guard-test', '~> 0.7.0'
# html parser
gem 'hpricot', '~> 0.8.6'
# ruby parser written in pure ruby
gem 'ruby_parser', '~> 3.1.2'
end
Due to the manual install of rails 4 I have activerecord in version 4.0.0 in my gems available. Why does it come up with that message? From my interpretation of the listed activerecord versions the dependency should be fulfilled with version 4.0.0? It is in the listed range between 3.0 and less than 5.

rails 5.1.4 console in production doesn't find models

I think I got a configuration issue with my rails setup in production.
I've got a puma working fine without any issues (connecting to db, loading models etc) and previously was able to use the console (I've encountered this [bug][https://github.com/rails/rails/issues/19256] but worked around it by filling in the database.yml)
When I try to open the console to perform little operations, none of my model classes are found.
✗ bundle exec rails console production
Running via Spring preloader in process 18313
Loading production environment (Rails 5.1.4)
irb(main):001:0> User.count
NameError: uninitialized constant User
from (irb):1
irb(main):002:0>
Similar questions on SO recommend to run ActiveRecord::Base.subclasses and eventually Rails.application.eager_load!
When I do try to run Rails.application.eager_load! I get uninitializaed constant error
irb(main):005:0> Rails.application.eager_load!
NameError: uninitialized constant ApplicationController
Did you mean? ApplicationCable
from app/controllers/accounts_controller.rb:1:in `<top (required)>'
from (irb):5
So I guess my console is now somehow lost and hasn't loaded the project source correctly.
How can I troubleshoot this situation ?
edit 2018-02-15 : I connected my workstation to the prod environment to use the console. This worked at first, the console was behaving correctly. After a couple times the same issues appeared. After doing spring stop I was able again to user my local workstation to open a console in prod.
Interesting fact : this never happens in dev environment, although spring had an app running in this environment.
It turns out spring is also running in prod, although the spring gem is only in dev group. (see below)
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
gem 'rails', '~> 5.1.4'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'therubyracer', platforms: :ruby
gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
gem 'rails-erd', require: false, group: :development
end
group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'pry'
gem 'pry-byebug'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'devise'
gem 'devise-i18n'
gem 'cancancan', '~> 2.0'
gem 'rolify'
gem 'redis', '~> 3.2'
group :production do
gem "sidekiq"
gem "sentry-raven"
end
gem 'pg'
gem "paperclip", "~> 5.0.0"
gem 'i18n-country-translations'
gem 'rails-i18n', '~> 5.0.0'
gem 'i18n_alchemy'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
If you deployed through Docker make sure using -ti prefix in running command like:
docker exec -ti 4d318d505e9f rails c production

Enki deployment to Heroku - problems with assets precompile

I cloned this https://github.com/xaviershay/enki and did the following modifications:
My Gemfile now looks like this:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
end
group :production do
gem 'pg'
end
group :development do
gem 'mysql2', '~> 0.3.11'
end
platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
gem 'trinidad'
gem 'jruby-openssl'
end
gem 'jquery-rails'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
# Bundle the extra gems:
gem 'RedCloth', '~> 4.2.9', :require => 'redcloth'
gem 'ruby-openid', :require => 'openid'
gem 'rack-openid', :require => 'rack/openid'
gem 'aaronh-chronic', :require => 'chronic' # Fixes for 1.9.2
gem 'coderay'
gem 'lesstile'
gem 'formtastic'
gem 'will_paginate', '~> 3.0.2'
gem 'exception_notification', '~> 2.5.2'
gem 'open_id_authentication'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :test do
gem 'database_cleaner'
gem 'cucumber-rails', :require => false
gem 'cucumber-websteps', :require => false
gem 'factory_girl'
gem 'rspec'
gem 'nokogiri', '~> 1.5.0'
gem 'webrat'
end
group :development, :test do
gem 'rspec-rails'
end
And I also modified the database.yml file to look like this:
development:
adapter: mysql2
database: myblog_development
username: root
password:
host: localhost
test:
adapter: mysql2
database: myblog_test
username: root
password:
host: localhost
I am able to check the site in my local, but when trying to deploy to Heroku I get the following:
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Tasks: TOP => environment
(See full trace by running task with --trace)
Precompiling assets failed, enabling runtime asset compilation
Injecting rails31_enable_runtime_asset_compilation
Please see this article for troubleshooting help:
http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting
I tried doing what they suggest in that last site, but still getting the same error. What am I missing?
In your application.rb file, add this line:
config.assets.initialize_on_precompile = false

Rails bundle install production only

I'm still new to rails/ruby/bundler and am a little confused.
In our config/application.rb file there's this bundler segment:
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
and in our Gemfile we use different groups, e.g.
group :development, :test do
gem "rspec-rails", ">= 2.7.0", :group => [:development, :test]
gem 'shoulda-matchers'
gem 'watchr'
gem 'spork', '~> 1.0rc'
gem 'spectator'
gem 'debugger'
gem 'wirble'
end
But when I run RAILS_ENV=production bundle install (or bundle install --deployment), it still installs gems from the development/test group...
Why does this happens or how can I make this work properly?
THIS ANSWER IS OUTDATED
Take a look at --without option:
bundle install --without development test
By default Bundler installs all gems and your application uses the gems that it needs. Bundler itself knows nothing about Rails and the current environment.
An alternative solution is to use the bundle-only ruby gem. It can be used as follows:
> gem install bundle-only
> bundle-only production
This library does not pollute your bundler configs or augment Gemfile.lock; it is a simple alternative to the built in bundle --without every other group option that bundler provides.

Does selenium with rspec have a pause or hold feature?

I'm using rspec and selenium. Is there soemthing like a pause button I can put in my test so I can interact with the page while I'm developing a test?
Now this is not the most optimal solution, but I found it useful to throw in a breakpoint statement in the middle of the test. This allows for inspection of the document in the browser or/and values inside your code.
I use pry for the debugging and have my Rails test environment set to not cache classes. This allows for quick modifications and browser (capybara forks a separate thread) refreshes. Here is a relevant portion of my Gemfile:
group :development, :test do
gem 'rspec-rails', github: 'rspec/rspec-rails'
gem "factory_girl_rails" # factorygirl instead of fixtures
gem "guard-rspec" # automaticly running the specs when file is saved
gem 'grizzled-rails-logger', github: 'bmc/grizzled-rails-logger'
gem "faker"
end
group :test do
gem "capybara", github: 'jnicklas/capybara' # simulating a user
gem 'database_cleaner' # cleans the database
gem 'launchy'
end # remember to install rb-fsevent if your on a mac
group :epic_console do
gem 'pry' # remember to look in development.rb
gem 'pry-rails'
gem 'pry-doc'
gem 'awesome_print'
gem 'hirb'
gem 'hirb-unicode'
gem 'pry-nav'
gem 'pry-coolline'
gem 'pry-stack_explorer'
gem 'pry-exception_explorer'
end
Google the gems, see if they can optimize your process.