【发布时间】:2016-06-12 17:42:24
【问题描述】:
我在使用 FactoryGirl on 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映射器),这里是涉及的文件:
app/models/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
spec/features/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
spec/factories/object_records.rb
FactoryGirl.define do
factory :obect_record do
object_id 1
object_type "ObjectA"
timestamp 1465748715
object_changes ({:property1 => "val1"})
end
end
spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/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
spec/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)>'
【问题讨论】:
标签: ruby-on-rails ruby factory-bot