我在Rails上的FactoryGirl遇到了一个奇怪的问题/错误:
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)>'
答案 0 :(得分:1)
ruby中存在一个现有的object_id
方法:您的工厂正在调用该方法,而不是直接转到工厂女孩中创建属性的method_missing
代码。
缺少钩子的方法只是一种方便,你可以通过调用
直接添加属性add_attribute :object_id, 1
此外,您的工厂定义似乎有拼写错误(obect_record
)。
由于object_id
是一种核心ruby方法,因此使用该名称会导致其他问题。