Ruby on rails 过程中的RSpec模型验证测试错误

Ruby on rails 过程中的RSpec模型验证测试错误,ruby-on-rails,rspec,Ruby On Rails,Rspec,我在RSpec中遇到了一个错误,我无法理解它为什么会出现错误。我使用FactoryBot创建一个扫描,并为fsp_name设置一个值。尝试验证第一个订单时,验证中不知何故无法识别此值 我得到的错误是 Failures: 1) Sweep validates Failure/Error: validates_presence_of :fsp_from, :fsp_to, :fsp_step, unless: -> (sweep) { sweep.fsp_name.empty?

我在RSpec中遇到了一个错误,我无法理解它为什么会出现错误。我使用FactoryBot创建一个扫描,并为fsp_name设置一个值。尝试验证第一个订单时,验证中不知何故无法识别此值

我得到的错误是

Failures:

  1) Sweep validates
     Failure/Error: validates_presence_of :fsp_from, :fsp_to, :fsp_step, unless: -> (sweep) { sweep.fsp_name.empty? }

     NoMethodError:
       undefined method `empty?' for nil:NilClass
     # ./app/models/sweep.rb:21:in `block in <class:Sweep>'
     # ./spec/models/sweep_spec.rb:21:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 0.8215 seconds (files took 0.37721 seconds to load)
1 example, 1 failure

工厂:sweep.rb

require 'rails_helper'

include Warden::Test::Helpers

describe Sweep do
  def create_sweep
    @user = FactoryBot.create(:user)
    @group = Group::PRIVATE
    @server = FactoryBot.create(:server, user: @user)
    @simulation = FactoryBot.create(:simulation, user: @user, group: @group, server: @server)
    @sweep = FactoryBot.create(:sweep, simulation: @simulation)
  end

  it "validates" do
    is_expected.to have_many :sweep_points
    is_expected.to belong_to :simulation
    is_expected.to have_one(:project).through :simulation
    is_expected.to belong_to :param_set

    create_sweep

    is_expected.to validate_numericality_of(:first_disorder).is_greater_than 0
  end
end
require 'faker'

FactoryBot.define do
  factory :sweep do
    first_disorder 1
    final_disorder 5
    fsp_name { Sweep::TESTER_VARIABLES.sample.first }
    fsp_from 1
    fsp_to 1
    fsp_step 1
    ssp_name "FLUENCE"
    ssp_from 1
    ssp_to 1
    ssp_step 1
    param_set_id 1
    state { Faker::Name.last_name }
    trait :empty do
    end
    trait :filled do
      param_set { create(:param_set, :filled) }
    end
  end
end
Class Sweep < ApplicationRecord
 has_many :sweep_points, dependent: :destroy
 belongs_to :simulation
 has_one :project, through: :simulation
 belongs_to :param_set, optional: true

 validates_associated :param_set

 TESTER_VARIABLES = [['Voltage','VOLTAGE'],['Fluence','FLUENCE'],['Alpha','ALPHA'],['Beta','BETA'],['Fermi level left','FERMI_LEVEL_LEFT'],['Fermi level right','FERMI_LEVEL_RIGHT'],['Seed for RNG','RANDSEED'],['Injection prefactor','INJECTION_PREFACTOR'],['Extraction prefactor','EXTRACTION_PREFACTOR'],['Singlet exciton binding energy','SINGLET_EXCITON_BINDING_ENERGY'],['Triplet exciton binding energy','TRIPLET_EXCITON_BINDING_ENERGY'],['Hole prefactor','HOLE_PREFACTOR'],['Electron prefactor','ELECTRON_PREFACTOR'],['CT-state binding energy','V'],['Minimal number of electrons','MIN_ELECTRONS'],['Minimal number of holes','MIN_HOLES'],['Minimal number of excitons','MIN_EXCITONS']]
 VARIABLES = [['Voltage','VOLTAGE']]

 validates :first_disorder, numericality: { only_integer: true, greater_than: 0, less_than: 1000 }
 validates :final_disorder, numericality: { only_integer: true, greater_than: 0, less_than: 1000 }
 validate :first_seq_final
 validates_presence_of :fsp_from, :fsp_to, :fsp_step, unless: -> (sweep) { sweep.fsp_name.empty? }
end

型号:sweep.rb

require 'rails_helper'

include Warden::Test::Helpers

describe Sweep do
  def create_sweep
    @user = FactoryBot.create(:user)
    @group = Group::PRIVATE
    @server = FactoryBot.create(:server, user: @user)
    @simulation = FactoryBot.create(:simulation, user: @user, group: @group, server: @server)
    @sweep = FactoryBot.create(:sweep, simulation: @simulation)
  end

  it "validates" do
    is_expected.to have_many :sweep_points
    is_expected.to belong_to :simulation
    is_expected.to have_one(:project).through :simulation
    is_expected.to belong_to :param_set

    create_sweep

    is_expected.to validate_numericality_of(:first_disorder).is_greater_than 0
  end
end
require 'faker'

FactoryBot.define do
  factory :sweep do
    first_disorder 1
    final_disorder 5
    fsp_name { Sweep::TESTER_VARIABLES.sample.first }
    fsp_from 1
    fsp_to 1
    fsp_step 1
    ssp_name "FLUENCE"
    ssp_from 1
    ssp_to 1
    ssp_step 1
    param_set_id 1
    state { Faker::Name.last_name }
    trait :empty do
    end
    trait :filled do
      param_set { create(:param_set, :filled) }
    end
  end
end
Class Sweep < ApplicationRecord
 has_many :sweep_points, dependent: :destroy
 belongs_to :simulation
 has_one :project, through: :simulation
 belongs_to :param_set, optional: true

 validates_associated :param_set

 TESTER_VARIABLES = [['Voltage','VOLTAGE'],['Fluence','FLUENCE'],['Alpha','ALPHA'],['Beta','BETA'],['Fermi level left','FERMI_LEVEL_LEFT'],['Fermi level right','FERMI_LEVEL_RIGHT'],['Seed for RNG','RANDSEED'],['Injection prefactor','INJECTION_PREFACTOR'],['Extraction prefactor','EXTRACTION_PREFACTOR'],['Singlet exciton binding energy','SINGLET_EXCITON_BINDING_ENERGY'],['Triplet exciton binding energy','TRIPLET_EXCITON_BINDING_ENERGY'],['Hole prefactor','HOLE_PREFACTOR'],['Electron prefactor','ELECTRON_PREFACTOR'],['CT-state binding energy','V'],['Minimal number of electrons','MIN_ELECTRONS'],['Minimal number of holes','MIN_HOLES'],['Minimal number of excitons','MIN_EXCITONS']]
 VARIABLES = [['Voltage','VOLTAGE']]

 validates :first_disorder, numericality: { only_integer: true, greater_than: 0, less_than: 1000 }
 validates :final_disorder, numericality: { only_integer: true, greater_than: 0, less_than: 1000 }
 validate :first_seq_final
 validates_presence_of :fsp_from, :fsp_to, :fsp_step, unless: -> (sweep) { sweep.fsp_name.empty? }
end
类扫描(sweep){sweep.fsp_name.empty?}
结束

在测试的唯一性时,我遇到了shoulda matchers的问题,该线程起到了帮助作用

我猜测试正在检查您尚未设置的主题,因此它正在自己创建一个扫描对象。此对象不会从factory bot生成,因此该字段将为
nil
,而不是
[]

尝试在“描述扫描”中添加此项:

主题{FactoryBot.create(:sweep,simulation:@simulation)}