Reputation: 499
Running RSpec on a Rails app: When valid?
is being called on an invalid record, RSpec crashes with the following output:
TypeError:
wrong argument type Hash (expected binding)
EDIT:
The problem was caused by the definition of a custom binding
method in app/helpers/application_helper.rb
.
This is very annoying because I cannot test my models.
Looking at the backtrace, it seems to me that the error doesn't come from my App code.
I tried to upgrade rspec-rails to the lattest version and to change the versions of i18n gem and some other that seemed related, but a bit at random for lack of anything better. It kept having the exact same message. I googled every set of terms that seemed relevant to me, but could not find anything comparable.
Would you know how to fix or proceed to fix this ?
I'm not looking for self-patching an error outside of my app code, but rather for fixing my configuration, app or tests, should the case arise.
Below, a test demonstrating this behaviour, command, backtrace, and Gemfile. Thank you in advance for your help.
Test:
1 require 'spec_helper'
2 #require 'rspec-expectations'
3
4 describe User do
5 before { @user = FactoryGirl.build(:user)}
6
7 subject { @user }
8
9 it { should be_valid}
10
11 describe "test" do
12 before { @user.email = "a" } #causing the model to be invalid
13
14 it "causes wrong argument type Hash (expected binding)" do
15 puts "here 1"
16 puts @user.method(:valid?).inspect
17 puts @user.valid?
18 puts "here 2"
19 end
20 end
21 end
In the console, it works as it should:
R ~/dev/pav/spec rails c test
Loading test environment (Rails 4.1.0)
2.1.0 :001 > user = FactoryGirl.build(:user)
=> #<User id: nil, name: "Example user1", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$04$V1znGRLKwr09y2k.QSoL2egSVNkedh22ZWnn1r9B31/...", remember_token: nil, guide: nil>
2.1.0 :002 > user.email = "a"
=> "a"
2.1.0 :003 > user.valid?
User Exists (5.8ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('a') LIMIT 1
=> false
Command, output and backtrace:
R ~/dev/pav rspec spec/models/user_spec.rb -b
.here 1
#<Method: User(ActiveRecord::Validations)#valid?>
F
Failures:
1) User test causes wrong argument type Hash (expected binding)
Failure/Error: puts @user.valid?
TypeError:
wrong argument type Hash (expected binding)
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/base.rb:176:in `eval'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/base.rb:176:in `load_rb'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/base.rb:166:in `load_file'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/base.rb:15:in `block in load_translations'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/base.rb:15:in `each'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/base.rb:15:in `load_translations'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/simple.rb:57:in `init_translations'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/backend/simple.rb:40:in `available_locales'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/config.rb:41:in `available_locales'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n/config.rb:47:in `available_locales_set'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n.rb:277:in `locale_available?'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n.rb:285:in `enforce_available_locales!'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/i18n-0.6.11/lib/i18n.rb:150:in `translate'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/naming.rb:187:in `human'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/errors.rb:430:in `generate_message'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/errors.rb:442:in `normalize_message'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/errors.rb:293:in `add'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validations/format.rb:32:in `record_error'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validations/format.rb:8:in `validate_each'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validator.rb:167:in `block in validate'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validator.rb:164:in `each'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validator.rb:164:in `validate'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:447:in `public_send'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:447:in `block in make_lambda'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:184:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:184:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `run_callbacks'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validations.rb:376:in `run_validations!'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validations/callbacks.rb:111:in `block in run_validations!'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:113:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:113:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:166:in `block in halting'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `run_callbacks'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validations/callbacks.rb:111:in `run_validations!'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activemodel-4.1.0/lib/active_model/validations.rb:317:in `valid?'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/activerecord-4.1.0/lib/active_record/validations.rb:70:in `valid?'
# ./spec/models/user_spec.rb:17:in `block (3 levels) in <top (required)>'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `instance_eval'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `block in run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `call'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:111:in `run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:390:in `block in run_examples'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `map'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `run_examples'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:371:in `run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `block in run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `map'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `map'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block in run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/reporter.rb:58:in `report'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:25:in `run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
# /Users/john/.rvm/gems/ruby-2.1.0@pav/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
Finished in 0.55629 seconds
2 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:14 # User test causes wrong argument type Hash (expected binding)
Randomized with seed 26201
Gemfile:
source 'http://bundler-api.herokuapp.com'
ruby '2.1.0'
gem 'rails', '4.1.0'
group :test do
gem 'capybara', '2.2.1'
end
gem 'rspec-rails', '2.14.2'
group :production do
gem 'rails_12factor'
end
gem 'pg'
gem 'bcrypt', '~> 3.1.5'
gem 'sass-rails', '~> 4.0.3'
gem 'coffee-rails', '~> 4.0.1'
gem 'jquery-rails', '~> 3.1.0'
gem 'turbolinks', '~> 2.2.2'
gem 'haml', '~> 4.0.5'
gem 'html2haml'
gem 'haml-rails', '~> 0.5.3'
gem 'sprockets', '2.11.0'
gem 'bootstrap-sass', '~> 3.1.1.0'
gem 'factory_girl_rails', '~> 4.4.1'
gem 'rails-i18n', '~> 4.0.2'
gem 'orderly'
gem 'uglifier'
gem 'hash_to_hidden_fields'
gem 'highcharts-rails', '~> 4.0.1'
gem 'mousetrap-rails'
gem 'jquery-turbolinks'
gem 'hashie'
Upvotes: 1
Views: 1482
Reputation: 29389
The code that is failing is as follows (from the top of your stacktrace):
# Loads a plain Ruby translations file. eval'ing the file must yield
# a Hash containing translation data with locales as toplevel keys.
def load_rb(filename)
eval(IO.read(filename), binding, filename)
end
In this context, binding
apparently evaluates to a Hash for some reason. You might try printing out that method here to see where it's coming from (e.g. puts method(:binding).inspect
) to see if you can figure out what is overriding the kernel definition.
As a related aside, this i18n gem is not the same gem as you are loading, as you can see by the version number of the source directory.
Upvotes: 1
Reputation: 40313
Not sure exactly what your problem is, but this mixing of instance variables with subject blocks looks weird, try this:
require 'spec_helper'
describe User do
subject { FactoryGirl.build(:user) }
it { should be_valid }
context "email test" do
subject { FactoryGirl.build(:user, email: 'a') }
it { should_not be_valid }
end
end
Upvotes: 1