Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails FactoryGirl Rails参数错误_Ruby On Rails_Ruby_Factory Bot - Fatal编程技术网

Ruby on rails FactoryGirl Rails参数错误

Ruby on rails FactoryGirl Rails参数错误,ruby-on-rails,ruby,factory-bot,Ruby On Rails,Ruby,Factory Bot,我在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' 注意:这不是一个重复的问题:-而且我已经尝试了很多可能的解决方案,比如手动加载或注意F

我在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'
注意:这不是一个重复的问题:-而且我已经尝试了很多可能的解决方案,比如手动加载或注意FactoryGirls和Spring的问题。但没有一个成功

设置是数据库基于MongoDB(Mongoid mapper),下面是涉及的文件:

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
等级库/特征/用户搜索等级库.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
规格/支架/工厂\女孩.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
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
等级库/rails\u 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
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)用户使用对象id搜索表
失败/错误:FactoryGirl.create(:对象\记录)
参数错误:
工厂未注册:对象\u记录
#./spec/features/user_搜索_spec.rb:8:in'block(2层)in'
ruby中有一个现有的方法:您的工厂正在调用该方法,而不是陷入创建属性的工厂女孩中缺少的
方法

缺少钩子的方法只是一种方便,您可以通过调用

add_attribute :object_id, 1
此外,您的工厂定义似乎有输入错误(
obect\u record


因为
object\u id
是一个核心的ruby方法,所以使用这个名称不可能会遇到其他问题。

伙计,你太棒了!谢谢我不吃这个。使用您的所有输入解决了问题。