I am trying to create a migration for a Ruby project using Sinatra. My model class is inside the app.rb file. When I run create_migration:
rake db:create_migration NAME=create_admins
I get the following exception with trace:
rake db:create_migration NAME=create_admins --trace
rake aborted!
Don't know how to build task 'db:create_migration'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task_manager.rb:62:in `[]'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:149:in `invoke_task'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
My app.rb is copied below:
require 'rubygems'
gem 'activerecord'
require 'sqlite3'
require 'active_record'
ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'w'))
#ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:host => "localhost",
:database => 'test6.db'
)
class Bill < ActiveRecord::Base
belongs_to :admin
end
class Admin < ActiveRecord::Base
validates :email, presence: true, uniqueness: true
validates :name, presence: true
has_many :bills
end
My Rakefile is copied below:
# Rakefile
require './app'
require 'sinatra'
require 'active_record'
#require 'rake'
Note: I am not using rails - I am using sinatra with activerecord
You can use sinatra-activerecord instead 'activerecord', and make minor changes like below -
in Gemfile
source 'https://rubygems.org'
gem "sinatra"
gem "pg"
gem "activerecord"
gem "sinatra-activerecord"
in rakefile.rb
require "./app"
require "sinatra/activerecord/rake"
This should work
Related
I am building a Ruby project that uses active record but not rails. Inside one of my tests I am trying the following:
it "fails with no driver name" do
command = "Driver"
expect {command_file.process_driver command}.to raise_error(ActiveRecord::RecordInvalid)
end
And here is the method I am trying to call
def process_driver command
driver_name = command.split[1]
Driver.create! :name => driver_name
end
I expect to be passing :name => nil to Driver.create! which should throw a RecordInvalid but instead I get I18n::InvalidLocaleData. Here is the backtrace
expected ActiveRecord::RecordInvalid, got #<I18n::InvalidLocaleData: can not load translations from /Users/me/.rbenv/versions/2.3.1/lib/r...ems/activesupport-5.1.3/lib/active_support/locale/en.yml: expects it to return a hash, but does not> with backtrace:
# ./command_file.rb:81:in `process_driver'
# ./command_file.rb:63:in `block in process'
# ./command_file.rb:51:in `each'
# ./command_file.rb:51:in `each_with_index'
# ./command_file.rb:51:in `process'
# ./spec/command_file_spec.rb:60:in `block (5 levels) in <top (required)>'
# ./spec/command_file_spec.rb:60:in `block (4 levels) in <top (required)>'
# ./spec/spec_helper.rb:75:in `block (3 levels) in <top (required)>'
# ./spec/spec_helper.rb:74:in `block (2 levels) in <top (required)>'
And here is my Gemfile
source 'https://rubygems.org'
gem 'sqlite3', '~> 1.3', '>= 1.3.13'
gem 'activerecord', '~> 5.1', '>= 5.1.3'
gem 'pry', '~> 0.10.4'
gem 'rspec', '~> 3.6'
gem 'factory_girl', '~> 4.5'
group :test do
gem 'database_cleaner'
end
I have no locale files of my own.
Any idea what's going on? I am not attempting any kind of translation in this project. I also don't understand why a locale file provided by active_support should fail. I'd be happy to simply disable i18n somehow if that were possible but I don't know how. Any ideas what the problem is?
For what ever reason :en was not set as my default locale. I fixed that in my spec_helper.rb by adding I18n.default_locale = 'en':
I18n.default_locale = 'en' # <--- add this line
RSpec.configure do |config|
# config here...
end
I realize this doesn't fix the larger problem of why the locale file from active_support was not loading, but my challenge was simply to make the error go away, not to use i18n
When running rspec to test a feature test using Sinatra Base, we are getting the following error.
This is how our feature tests looks
require 'capybara/rspec'
feature 'Enter names' do
scenario 'submitting names' do
visit('/')
fill_in :player_1_name, with: 'Dave'
fill_in :player_2_name, with: 'Mittens'
click_button 'Submit'
expect(page).to have_content 'Dave vs. Mittens'
end
end
This is our Gemfile:
source 'https://rubygems.org'
gem 'sinatra'
gem 'rspec-sinatra'
gem 'capybara'
error:
1.1) Failure/Error: visit "/"
ArgumentError:
rack-test requires a rack application, but none was given
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rack_test/driver.rb:16:in `initialize'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `new'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `block in <top (required)>'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `call'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `driver'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:227:in `visit'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/feature/feature_spec.rb:5:in `block (2 levels) in <top (required)>'
1.2) Failure/Error: Unable to find matching line from backtrace
ArgumentError:
rack-test requires a rack application, but none was given
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rack_test/driver.rb:16:in `initialize'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `new'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `block in <top (required)>'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `call'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `driver'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:103:in `reset!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `block in reset_sessions!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `each'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `reset_sessions!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rspec.rb:20:in `block (2 levels) in <top (required)>'
We've tried requiring the spec_helper file in our feature test, and also requiring our app.rb in the feature test and we get a different error saying it cannot load sinatra-base
Here is our spec_helper
ENV['RACK_ENV'] = 'test'
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require 'capybara'
require 'capybara/rspec'
require 'rspec'
Capybara.app = Battle
RSpec.configure do |config|
config.include Capybara::DSL
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Any direction would be massively appreciated.
Thanks
Russ
Have a look here:
http://recipes.sinatrarb.com/p/testing/rspec
It suggests, among other things, adding the following to your spec_helper.rb:
module RSpecMixin
include Rack::Test::Methods
def app() Sinatra::Application end
end
RSpec.configure do |config|
config.include RSpecMixin
end
I have a very simple unit test that I copied from http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing.
require_relative "simple_number"
require "test/unit"
class TestSimpleNumber < Test::Unit::TestCase
def test_simple
assert_equal(4, SimpleNumber.new(2).add(2) )
assert_equal(6, SimpleNumber.new(2).multiply(3) )
end
end
Executing this code causes error:
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
From:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:3:in `<top (required)>'
tc_simple_number.rb:5:in `<main>'
MiniTest::Unit::TestCase is now Minitest::Test. From /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit/testcase.rb:8:in `<module:Unit>'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:670:in `<class:Runner>': undefined method `_run_suite' for class `Test::Unit::Runner' (NameError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:255:in `<module:Unit>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:9:in `<module:Test>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:8:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from tc_simple_number.rb:5:in `<main>'
What might be wrong?
Test::Unit::TestCase is deprecated in favor of Minitest::Test. If you want your example to work you need to change a few things:
replace require "test/unit" with require "minitest/autorun"
replace Test::Unit::TestCase with with Minitest::Test
Here is a little code snippet that I am trying to use to implement gem activeuuid:
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.1.1'
gem 'pg'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
gem 'activeuuid'
User.rb
class User < ActiveRecord::Base
include ActiveUUID::UUID
end
Migration xxxxxxxxx_create_users.rb
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users, :id => false do |t|
t.uuid :id, :primary_key => true
t.string :email
t.timestamps
end
end
def self.down
drop_table :users
end
end
Tyring a simple query is not proving to be fisible here even after bundle or restart of console ..
2.1.1 :016 > User.all
NoMethodError: undefined method `set_primary_key' for User (call 'User.connection' to establish a connection):Class
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activerecord-4.1.1/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activeuuid-ps-0.1.2/lib/activeuuid/uuid.rb:54:in `block in <module:UUID>'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/concern.rb:120:in `class_eval'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/concern.rb:120:in `append_features'
from /home/sahil/projects/test_projects/myapp/app/models/user.rb:3:in `include'
from /home/sahil/projects/test_projects/myapp/app/models/user.rb:3:in `<class:User>'
from /home/sahil/projects/test_projects/myapp/app/models/user.rb:1:in `<top (required)>'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:443:in `load'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:443:in `block in load_file'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:633:in `new_constants_in'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:442:in `load_file'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:342:in `require_or_load'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:480:in `load_missing_constant'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:180:in `const_missing'
from (irb):16
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
or even
2.1.1 :029 > User.count
NoMethodError: undefined method `set_primary_key' for User (call 'User.connection' to establish a connection):Class
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activerecord-4.1.1/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from /home/sahil/.rvm/gems/ruby-2.1.1#myapp/gems/activeuuid-ps-0.1.2/lib/activeuuid/uuid.rb:54:in `block in <module:UUID>'
..........
..........
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Whats happening wrong here. Am i missing some required dependency.
Found out a better solution and implemented as show at : http://www.codesapling.com/blog/2014/05/24/using-uuid-as-primary-key-in-rails4-with-postgres/
I'v installed Rails 3.1 new application with some gems:
source 'http://rubygems.org'
gem 'rails', '3.1.1'
gem 'sqlite3', '1.3.4'
group :assets do
gem 'sass-rails', '~> 3.1.4'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'unicorn', '4.1.1'
gem execjs, 1.2.9
gem therubyracer, '0.9.9'
gem 'gravatar_image_tag', '1.0.0'
group :test do
# Pretty printed test output
gem 'turn', '0.8.3', :require => true
gem 'rspec-rails', '2.7.0'
gem 'syntax', '1.0.0'
gem 'factory_girl_rails', '1.3.0'
end
group :development do
gem 'webrat', '0.7.3'
gem 'rspec-rails', '2.7.0'
gem 'syntax', '1.0.0'
end
group :production do
end
$ bundle exec rails new calculator -T
$ cd calculator
$ bundle exec rails g rspec:install
Create file
require 'spec_helper'
describe PagesController do
render_views
before(:each) do
#base_title = "Calculator Tutorial Application | "
end
describe "GET 'home'" do
it "returns http success" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => #base_title + "Home")
end
end
end
and run test
$ ./bin/rspec spec/controllers/pages_controller_spec.rb
/usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require': can't convert true into String (TypeError)
from /usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
from /usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
from /usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require'
from /usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
from /usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
from /usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
from /home/ror/calculator/config/application.rb:13:in `'
from /home/ror/calculator/config/environment.rb:2:in `require'
from /home/ror/calculator/config/environment.rb:2:in `'
from /home/ror/calculator/spec/spec_helper.rb:3:in `require'
from /home/ror/calculator/spec/spec_helper.rb:3:in `'
from /home/ror/calculator/spec/controllers/pages_controller_spec.rb:1:in `require'
from /home/ror/calculator/spec/controllers/pages_controller_spec.rb:1:in `'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `load'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `block in load_spec_files'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `map'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `load_spec_files'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:18:in `run'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in `run_in_process'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:69:in `run'
from /home/ror/.gem/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in `block in autorun'
After comment line 13 in ./config/application.rb : # Bundler.require(*Rails.groups(:assets => %w(development test))) Rspec test system works fine, but I have another problem with gravatar_image_tag which works only comment out line Bundle.requre.
All gems are installed over bundler in user home directory.
Answer from user486421 himself:
Disabling 'turn' gem in Gemfile solved the problem:
gem 'turn', '0.8.3', :require => false