在Rails应用程序上运行RSpec:在无效记录上调用valid?
时,RSpec会崩溃并显示以下内容:
TypeError:
wrong argument type Hash (expected binding)
修改
问题是由binding
中自定义app/helpers/application_helper.rb
方法的定义引起的。
这非常烦人,因为我无法测试我的模型。
查看回溯,在我看来,错误并非来自我的应用程序代码。
我尝试将rspec-rails升级到最新版本并更改i18n gem和其他一些看似相关的版本,但有点随意因为缺少更好的东西。它保持完全相同的消息。我搜索了与我相关的每一组术语,但找不到任何可比较的术语。
您知道如何修复或继续修复此问题吗?
我不是在我的应用代码之外寻找自我修补错误,而是在案件出现时修复我的配置,应用或测试。
下面是一个测试,演示了这种行为,命令,回溯和Gemfile。 提前感谢您的帮助。
测试
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
在控制台中,它可以正常工作:
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: "Example.user1@example.com", 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
命令,输出和回溯:
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'
答案 0 :(得分:1)
不确定你的问题究竟是什么,但实例变量与主题块的混合看起来很奇怪,试试这个:
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
答案 1 :(得分:1)
失败的代码如下(从堆栈跟踪的顶部开始):
# 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
在这种情况下,binding
显然由于某种原因评估为哈希。您可以尝试在此处打印出该方法以查看它的来源(例如puts method(:binding).inspect
),看看是否可以找出覆盖内核定义的内容。
作为相关的一点,这个i18n gem与你加载的宝石不同,正如您可以通过源目录的版本号看到的。