2016-06-12 96 views
0

我有FactoryGirl on Rails的一个奇怪的问题/错误:FactoryGirl Rails的参数错误

ArgumentError: wrong number of arguments (given 1, expected 0) 
    /Users/bintoy/ruby-apps/tradegecko-exercise/exercise-app/spec/factories/object_records.rb:3:in `object_id' 

注意:这是不是从这个重复的问题:Why am I getting FactoryGirl wrong number of arguments error? - 而且我已经尝试了很多类似可能的解决方案手动加载或注意FactoryGirls和Spring的问题。但他们都没有工作。

的设置是,数据库是基于MongoDB的(Mongoid映射器),这里是所涉及的文件:

应用程序/模型/ object_record.rb

class ObjectRecord 
    include Mongoid::Document 

    validates_presence_of :object_id, :object_type, :timestamp, :object_changes 
    validates_uniqueness_of :timestamp, :scope => [:object_id, :object_type] 

    field :object_id, type: Integer 
    field :object_type, type: String 
    field :timestamp, type: DateTime 
    field :object_changes, type: Hash 

end 

规格/功能/ user_searches_spec.rb

require 'spec_helper' 
require 'rails_helper' 

    feature 'User searches on table' do 
     before :each do 
     FactoryGirl.create(:object_record) 
     end 

     scenario 'with an object id', js: true do 
     input_a_search '1' 

     expect(page).to have_css(".sorting_1", :text => "1") 
     save_and_open_screenshot 

     end 

     def input_a_search(search_word) 
     visit object_records_index_path 
     find('input[type=search]').set(search_word) 
     end 
    end 

规格/工厂/ object_records.rb

FactoryGirl.define do 
    factory :obect_record do 
     object_id 1 
     object_type "ObjectA" 
     timestamp 1465748715 
     object_changes ({:property1 => "val1"}) 
    end 
end 

规格/支持/ factory_girl.rb

RSpec.configure do |config| 
     config.include FactoryGirl::Syntax::Methods 
    end 

规格/ spec_helper.rb

RSpec.configure do |config| 

    config.after(:all) do 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:suite) do 
    FactoryGirl.reload 
    end 


    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 

规格/ rails_helper.rb

ENV['RAILS_ENV'] ||= 'test' 
require File.expand_path('../../config/environment', __FILE__) 

abort("The Rails environment is running in production mode!") if Rails.env.production? 
require 'spec_helper' 
require 'rspec/rails' 

require 'capybara/rspec' 
require 'capybara/rails' 

require 'capybara/poltergeist' 
Capybara.javascript_driver = :poltergeist 

require 'factory_girl_rails' 


Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 

RSpec.configure do |config| 
    config.include Capybara::DSL 

    config.infer_spec_type_from_file_location! 

    config.filter_rails_from_backtrace! 

end 

编辑: 出只是一个随机的尝试为了调试这个,我试着在工厂中省略object_id的参数来看看会发生什么:

FactoryGirl.define do 
    factory :obect_record do 
     object_id 
... 

而是我得到了Rspec的这个故障/错误消息:

1) User searches on table with an object id 
    Failure/Error: FactoryGirl.create(:object_record) 

    ArgumentError: 
     Factory not registered: object_record 
    # ./spec/features/user_searches_spec.rb:8:in `block (2 levels) in <top (required)>' 

回答

1

有一个现有的object_id方法红宝石:你的工厂调用该方法,而不是通过掉落到method_missing代码工厂女孩,创造一个属性。

方法缺少挂钩只是一个方便,可以直接致电

add_attribute :object_id, 1 

除了你的工厂的定义似乎有一个错字(obect_record)添加属性。

由于object_id是一个核心ruby方法,因此使用该名称会遇到其他问题并不是不可能的。

+0

伙计,你摇滚!谢谢!我会采取一个不是这个。使用所有输入解决问题。 – Raven