我遇到了RSpec的错误,我无法弄清楚为什么会出现这个错误。我使用FactoryBot创建一个扫描,为fsp_name设置一个值。尝试验证first_order时,在验证中以某种方式无法识别此值。
我得到的错误是
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时,@ sweep.fsp_name存在并返回类似'Voltage'的值。我不明白为什么测试失败了。
RSpec测试:sweep_spec.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
工厂:sweep.rb
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
型号:sweep.rb
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
在测试该线程的唯一性时,我遇到了与shoulda-matchers有关的问题。 Rspec validates_uniqueness_of test failing with additional validation errors
我猜测测试是检查你没有设置的主题,所以它自己创建一个扫描对象。这个对象不会从工厂机器人生成,因此该字段将是nil
而不是[]
。
尝试在描述Sweep:
subject { FactoryBot.create(:sweep, simulation: @simulation) }
中添加此内容