Active Record unexpected i18n error - ruby

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

Related

After running bundle install I got this message "cannot load such file -- appium_lib"

I was running my codes without any issues while I was only automating my wep app.
Now I wanted to start automating my mobile app and after running bundle install I'm getting this error. I don't know where else I can find the solution for this error.
I guess is some gem that is not correct installed but already removed all gems and it didn't work.
cannot load such file -- appium_lib (LoadError)
/Users/brunobacelar/Curatio/Automation/ui_tests/iOS/features/support/env.rb:3:in `require'
/Users/brunobacelar/Curatio/Automation/ui_tests/iOS/features/support/env.rb:3:in `<top (required)>'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/glue/registry_and_more.rb:121:in `require'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/glue/registry_and_more.rb:121:in `load_code_file'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/runtime/support_code.rb:142:in `load_file'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/runtime/support_code.rb:81:in `block in load_files!'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/runtime/support_code.rb:80:in `each'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/runtime/support_code.rb:80:in `load_files!'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/runtime.rb:260:in `load_step_definitions'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/runtime.rb:72:in `run!'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/lib/cucumber/cli/main.rb:29:in `execute!'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/gems/cucumber-7.0.0/bin/cucumber:9:in `<top (required)>'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/bin/cucumber:23:in `load'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/bin/cucumber:23:in `<main>'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `eval'
/Users/brunobacelar/.rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `<main>'
env.rb
# frozen_string_literal: true
require 'appium_lib'
require 'rubygems'
require 'pry'
# creating an array from the data in the YML file
APPCONSTANT=YAML.load_file('/Users/brunobacelar/Curatio/Automation/ui_tests/iOS/config/appConstants.yml')
def caps { caps: {
platformVersion: "14.5",
deviceName: "iPhone SE2",
platformName: 'iOS',
automationName: 'XCUITest',
bundleId: "me.curatio.st-i",
udid: "XXXXXXXX-XXXXXXXXXXXX",
newCommandTimeOut: '3600',
xcodeOrgId: 'XXXXXXXXXXX',
xcodeSigningId: 'iPhone Developer',
noReset: true } } end
# $driver = Appium::Driver.new(caps, true) Appium::Driver.new(caps, true) Appium.promote_appium_methods Object
there you go. You've answered your own question. It cannot load appium_lib which you have specified on line3 of your env.rb.
Usually you would ensure this is inside your Gemfile or suchlike. Double check that your Gemfile.lock has the relevant gems, your ruby version is correct for where you're running and that you also are requiring the right path

Nomethod error when trying to make rr mock stubs work

I am setting up a project and have been trying to make rr mocks work but I am getting nomethod error. I have tried moving the required method as well but nothing works. This is my repo
$ rspec spec/views
F
Failures:
1) home/show.html.haml
Failure/Error: stub(view).user { user }
NoMethodError:
undefined method `stub' for #<RSpec::ExampleGroups::HomeShowHtmlHaml:0x007fc245188f08>
# ./spec/views/home/show.html.haml_spec.rb:6:in `block (2 levels) in <top (required)>'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
I have tried whatever was mentioned here rr
# Gemfile
group :test do
gem 'rr', require: false
end
# test helper
require File.expand_path('../../config/environment', __FILE__)
require 'your/test/framework' # if you are using something other than MiniTest / Test::Unit
require 'rr'
I got this working finally, I used gem 'rr', '1.1.2' instead of 1.2.0 which got auto installed when running bundler without the gem version.

Sinatra rspec capybara: "rack test requires a rack application"

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

Is activeuuid and rails 4.1.1 compatible or there is something wrong here?

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/

Is it bug in bundler and rspec?

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

Resources