Overall Description of Problem:
My fixture for my WeightClass model is returning nil during testing. However, I know the fixtures are valid because when I run rails db:fixtures:load RAILS_ENV=test, all of my fixtures are successfully loaded into my test database.
On a side note, if anybody could point me to the Rails source code that deals with how the weight_classes method (and other methods that turn fixtures into model classes) works in a test, it would be greatly appreciated.
Relevant Files
I have a model for weight classes as seen below (app/models/weight_class.rb):
class WeightClass < ApplicationRecord
validates :name, presence: true, length: { maximum: 15 }
validates :name_abbr, presence: true, length: { maximum: 3 }
validates :gender, presence: true, length: { maximum: 1 }
validates :lower_bound, numericality: { greater_than: 0 }, allow_nil: true
validates :upper_bound, numericality: { greater_than: 0 }, allow_nil: true
validates_with LowerOrUpperBoundRequiredValidator
validates_with RangeValidator, if: :lower_and_upper_bound_present?
private
def lower_and_upper_bound_present?
self.lower_bound and self.upper_bound
end
end
And it's migration here:
class CreateWeightClasses < ActiveRecord::Migration[5.0]
def change
create_table :weight_classes do |t|
t.string :name, limit: 15, null: false
t.string :name_abbr, limit: 3, null: false
t.string :gender, limit: 1, null: false
t.decimal :lower_bound, precision: 5, scale: 2
t.decimal :upper_bound, precision: 5, scale: 2
t.timestamps
end
end
end
The fixture in question here (test/fixtures/weight_classes.yml):
middleweight_men:
name: Middleweight
name_abbr: MW
gender: m
lower_bound: 73
upper_bound: 87
The problem is, when I call this fixture in a test to see if it's valid:
require 'test_helper'
class WeightClassTest < ActiveSupport::TestCase
def setup
@middlewight_men = weight_classes(:middleweight_men)
end
test "should be valid" do
assert @middleweight_men.valid?
end
end
I get this error:
NoMethodError: undefined method `valid?' for nil:NilClass
Aucun commentaire:
Enregistrer un commentaire