2014-07-19 2 views
1

Запуск RSpec на приложение Rails: Когда valid? вызывается на недопустимый записи, RSpec падает со следующим выходом:Запуск RSpec вызывает неправильный тип аргумента

TypeError: 
    wrong argument type Hash (expected binding) 

EDIT: Проблема была вызвана определение пользовательского метода 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: "[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 

команд, выход и трассировку:

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/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/base.rb:176:in `eval' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/base.rb:176:in `load_rb' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/base.rb:166:in `load_file' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/base.rb:15:in `block in load_translations' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/base.rb:15:in `each' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/base.rb:15:in `load_translations' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/simple.rb:57:in `init_translations' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/backend/simple.rb:40:in `available_locales' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/config.rb:41:in `available_locales' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n/config.rb:47:in `available_locales_set' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n.rb:277:in `locale_available?' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n.rb:285:in `enforce_available_locales!' 
    # /Users/john/.rvm/gems/[email protected]/gems/i18n-0.6.11/lib/i18n.rb:150:in `translate' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/naming.rb:187:in `human' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/errors.rb:430:in `generate_message' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/errors.rb:442:in `normalize_message' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/errors.rb:293:in `add' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validations/format.rb:32:in `record_error' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validations/format.rb:8:in `validate_each' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validator.rb:167:in `block in validate' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validator.rb:164:in `each' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validator.rb:164:in `validate' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:447:in `public_send' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:447:in `block in make_lambda' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:184:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:184:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:185:in `block in simple' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `run_callbacks' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validations.rb:376:in `run_validations!' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validations/callbacks.rb:111:in `block in run_validations!' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:113:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:113:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:166:in `block in halting' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `run_callbacks' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validations/callbacks.rb:111:in `run_validations!' 
    # /Users/john/.rvm/gems/[email protected]/gems/activemodel-4.1.0/lib/active_model/validations.rb:317:in `valid?' 
    # /Users/john/.rvm/gems/[email protected]/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/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `instance_eval' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `block in run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:179:in `run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec' 
    # /Users/john/.rvm/gems/[email protected]/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/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:247:in `instance_eval_with_args' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `call' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:108:in `run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/hooks.rb:446:in `run_hook' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:256:in `with_around_each_hooks' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:111:in `run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:390:in `block in run_examples' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `map' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `run_examples' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:371:in `run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `block in run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `map' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:372:in `run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `map' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block in run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/reporter.rb:58:in `report' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:25:in `run' 
    # /Users/john/.rvm/gems/[email protected]/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run' 
    # /Users/john/.rvm/gems/[email protected]/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' 

ответ

1

код, который не удается как следует (от верхней части 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 

в этом контексте, по-видимому, binding вычисляется в Hash по какой-то причине. Вы можете попробовать распечатать этот метод здесь, чтобы узнать, откуда он (например, puts method(:binding).inspect), чтобы узнать, можете ли вы определить, что переопределяет определение ядра.

В отличие от этого, этот драгоценный камень i18n не является тем же самым камнем, который вы загружаете, как вы можете видеть по номеру версии исходного каталога.

+0

i18n - зависимость рельсов-i18n.Снижение рейтинга i18n не помогло. 'method (: binding) .inspect' дает' I18n :: Backend :: Simple (ApplicationHelper) # binding', который кажется нормальным. Спасибо за Ваш ответ! – wacha

+0

Как вы заключаете, что это определение 'binding' является нормальным? Похоже, что это происходит от 'ApplicationHelper', а не от' Kernel', что является нормальным источником его в Rails. –

+0

Я действительно не специалист по этому вопросу. Это казалось мне правдоподобным, потому что оно происходит от драгоценного камня от svenfuchs (который также участвует в rails-i18n), что является зависимостью драгоценного камня I18n в модуле [I18n] (https://github.com/svenfuchs/i18n/blob /master/lib/i18n/backend/base.rb), и потому, что я предполагаю, что RSpec должен сделать некоторые магии для запуска тестов. – wacha

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 
+0

Это не решило проблему. Спасибо за рекомендацию! – wacha

Смежные вопросы