Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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时未定义的方法。_Ruby On Rails_Ruby_Rspec_Factory Bot - Fatal编程技术网

Ruby on rails 创建对象FactoryGirl时未定义的方法。

Ruby on rails 创建对象FactoryGirl时未定义的方法。,ruby-on-rails,ruby,rspec,factory-bot,Ruby On Rails,Ruby,Rspec,Factory Bot,我为我的模型民意测验编写测试。选中,使用嵌套的atributes vote_选项创建对象轮询。这是我的工厂。rb: FactoryGirl.define do factory :vote_option do title "Ruby" poll end factory :poll do topic "What programming language are you using?" trait :vote_option1 do asso

我为我的模型民意测验编写测试。选中,使用嵌套的atributes vote_选项创建对象轮询。这是我的工厂。rb:

FactoryGirl.define do

  factory :vote_option do
    title "Ruby"
    poll
  end

  factory :poll do
    topic "What programming language are you using?"

    trait :vote_option1 do
      association :vote_option, title: "C#"
    end

    trait :vote_option2 do
      association :vote_option, title: "Ruby"
    end

    factory :poll_with_vote1, traits: [:vote_option1]
    factory :poll_with_vote2, traits: [:vote_option2]
  end
end   
我创建了测试和检查,那个对象被创建了。 民意测验_srec.rb

require 'rails_helper'

RSpec.describe Poll, type: :model do
  #let(:user) { FactoryGirl.create(:user) }

  before do
    @poll = FactoryGirl.create(:poll_with_vote1)

  end

  subject{@poll}

  it { should be_valid }

end
我运行poll_spec.rb并给出一个错误: 1)投票 失败/错误:@poll=FactoryGirl.create(:poll_with_vote1) 命名错误: 对于#

为什么会出现这种错误?我的工厂怎么了

Just in case model Poll:
class Poll < ActiveRecord::Base
  VOTE_OPTIONS_MIN_COUNT = 1

  has_many :vote_options, dependent: :destroy
  has_many :votes

  validates :topic, presence: true
  #validates :vote_options, presence: true #association_count: { minimum: VOTE_OPTIONS_MIN_COUNT }


  #validates :user_id, presence: true

  accepts_nested_attributes_for :vote_options, :reject_if => :all_blank,
                                                  :allow_destroy => true

  def normalized_votes_for(option)
    votes_summary == 0 ? 0 : (option.votes.count.to_f / votes_summary) * 100
  end

  def votes_summary
    vote_options.inject(0) { | summary, option | summary + option.votes.count  }
  end
end
以防万一模型轮询:
类轮询:全部\u为空,则接受:拒绝\u,
:allow_destroy=>true
def标准化投票(选项)
投票结果汇总=0?0:(option.voces.count.to_f/voces_summary)*100
结束
def投票汇总表
vote_options.inject(0){| summary,option | summary+option.vots.count}
结束
结束

出现此错误的原因是您与此名称没有关联。尝试:

FactoryGirl.define do

  factory :vote_option do
     title "Ruby"
  end

  factory :poll do

    topic "What programming language are you using?"

    trait :vote_option1 do
      after(:create) {|poll| poll.vote_options << create(:vote_option, title: 'C#')         
    end

    trait :vote_option2 do
      after(:create) {|poll| poll.vote_options << create(:vote_option, title: 'Ruby')         
    end

    factory :poll_with_vote1, traits: [:vote_option1]
    factory :poll_with_vote2, traits: [:vote_option2]
  end
end

非常感谢。现在很好,但显示错误:Failure/error:@poll=FactoryGirl.create(:poll_with_vote1)ActiveRecord::RecordInvalid:验证失败:投票选项不能为空为什么工厂日期为空?尝试将(:create)之后的所有
更改为(:build)
谢谢!请您解释一下语法:after(:build){| poll | poll.vote_选项好吗
FactoryGirl.define do

  factory :vote_option do
     title { FFaker::Lorem.word }
  end

  factory :poll do

    topic { FFaker::Lorem.sentence + '?' }

    transient do    # In old version `ignore do`
      vote_options_number { rand(1..4) }
    end

    after(:build) do |poll, ev|
      poll.vote_options << build_list :vote_option, ev.vote_options_number
    end

end