Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:44:35+00:00 2026-06-08T20:44:35+00:00

I am creating a Rails app (fairly new at this), and I have a

  • 0

I am creating a Rails app (fairly new at this), and I have a Client model that I generated without using scaffolding. I have run ‘rake db:migrate’, and I am unit testing the model with ‘rake test:units’, however I am getting variations of the following runtime error in terminal for practically all of my factory testing.

test: Creating seven clients should show that all factories are properly created. (ClientTest):
NoMethodError: undefined method `destroy' for nil:NilClass
    /Users/myUserName/Desktop/app_name/test/unit/client_test.rb:131:in `block (2 levels) in <class:ClientTest>'

I have been unable to figure out what the error is. I understand that it doesn’t recognize the client class and thus it can’t find the ‘destroy’ method for nil. However, I’m unsure of how to fix it.

Below is my model code, located in app_name/app/models/client.rb

class Client < ActiveRecord::Base
  # Callbacks
  before_save :reformat_phone

  # Relationships
  has_many :assignments
  has_many :counselors, :through => :assignments
  has_many :interventions, :through => :assignments

  # Validations
  validates_presence_of :first_name, :last_name, :gender, :address, :city, :state, :zip, :phone, :active
  validates_inclusion_of :gender, :in => %w[male female], :message => "is not an option"
  validates_inclusion_of :marital_status, :in => %w[single married separated divorced], :message => "is not an option"
  validates_inclusion_of :state, :in => %w[PA OH WV], :message => "is not an option"
  validates_format_of :zip, :with => /^\d{5}$/, :message => "should be five digits long"
  validates_format_of :phone, :with => /^\(?\d{3}\)?[-. ]?\d{3}[-.]?\d{4}$/, :message => "should be 10 digits (area code needed) and delimited with dashes only"

  # Scopes
  scope :active, where('active = ?', true)
  scope :inactive, where('active = ?', false)
  scope :alphabetical, order('last_name, first_name')

  scope :receiving_gov_assistance, where('gov_assistance = ?', true)
  scope :not_receiving_gov_assistance, where('gov_assistance = ?', false)

  scope :male, where('gender = ?', 'male')
  scope :female, where('gender = ?', 'female')

  scope :by_marital_status, lambda { |status| where("marital_status = ?", status) }
  scope :by_ethnicity, lambda { |race| where("ethnicity = ?", race) }

  scope :employed, where('is_employed = ?', true)
  scope :unemployed, where('is_employed = ?', false)

  scope :veteran, where('is_veteran = ?', true)

  scope :assigned, where('current_assignment != ?', nil)
  scope :unassigned, where('current_assignment = ?', nil) 

# Other methods
  def name
    "#{last_name}, #{first_name}"
  end

  def proper_name
    "#{first_name} #{last_name}"
  end

  def current_assignment
    curr_assignment = self.assignments.select{|a| a.end_date.nil?}
    # alternative method for finding current assignment is to use scope 'current' in assignments:
    # curr_assignment = self.assignments.current    # will also return an array of current assignments
    return nil if curr_assignment.empty?
    curr_assignment.first   # return as a single object, not an array
  end

  # Misc Constants
  GENDER_LIST = [['Male', 'male'],['Female', 'female']]
  STATES_LIST = [['Ohio', 'OH'],['Pennsylvania', 'PA'],['West Virginia', 'WV']]

  # Callback code
  # -----------------------------
  private
  def reformat_phone
    phone = self.phone.to_s  # change to string in case input as all numbers 
    phone.gsub!(/[^0-9]/,"") # strip all non-digits
    self.phone = phone       # reset self.phone to new string
  end
end

And here are the tests I’ve written, located in app_name/test/unit/client_test.rb

require 'test_helper'

class ClientTest < ActiveSupport::TestCase
  # Test relationships
  should have_many(:assignments)
  should have_many(:deacons).through(:assignments)
  should have_many(:interventions).through(:assignments)

  # Test basic validations
  should validate_presence_of(:last_name)
  should validate_presence_of(:first_name)
  should validate_presence_of(:gender)
  should validate_presence_of(:address)
  should validate_presence_of(:city)
  should validate_presence_of(:state)
  should validate_presence_of(:zip)
  should validate_presence_of(:phone)
  should validate_presence_of(:active)

  # Identity-based tests
      # tests for gender
      should allow_value("male").for(:gender)
      should allow_value("female").for(:gender)

      should_not allow_value(nil).for(:gender)
      should_not allow_value(1).for(:gender)
      should_not allow_value("seahorse").for(:gender)
      should_not allow_value("I believe gender is a societal construct.").for(:gender)

      # tests for ethnicity
      should allow_value("Asian").for(:ethnicity)
      should allow_value("Black").for(:ethnicity)
      should allow_value("Hispanic").for(:ethnicity)
      should allow_value("Latino").for(:ethnicity)
      should allow_value("Native American").for(:ethnicity)
      should allow_value("White").for(:ethnicity)

      should_not allow_value(nil).for(:ethnicity)
      should_not allow_value(1).for(:ethnicity)
      should_not allow_value(true).for(:ethnicity)
      should_not allow_value(0.5).for(:ethnicity)

      # tests for marital status
      should allow_value("single").for(:marital_status)
      should allow_value("married").for(:marital_status)
      should allow_value("separated").for(:marital_status)
      should allow_value("divorced").for(:marital_status)

      should_not allow_value("White").for(:marital_status)
      should_not allow_value(nil).for(:marital_status)
      should_not allow_value(1).for(:marital_status)
      should_not allow_value(true).for(:marital_status)
      should_not allow_value("I believe marriage is a societal construct.").for(:marital_status)

  # Contact-based Tests
      # tests for address
      should allow_value("123 Example Lane").for(:address)
      should allow_value("456 Another Street").for(:address)

      should_not allow_value(true).for(:address)
      should_not allow_value(101).for(:address)
      should_not allow_value(nil).for(:address)

      # tests for zip
      should allow_value("12345").for(:zip)

      should_not allow_value("bad").for(:zip)
      should_not allow_value("1234").for(:zip)
      should_not allow_value("123456").for(:zip)
      should_not allow_value("12345-6789").for(:zip)

      # tests for state
      should allow_value("OH").for(:state)
      should allow_value("PA").for(:state)
      should allow_value("WV").for(:state)
      should_not allow_value("bad").for(:state)
      should_not allow_value("NY").for(:state)
      should_not allow_value(10).for(:state)
      should_not allow_value("CA").for(:state)

      # tests for phone
      should allow_value("4122683259").for(:phone)
      should allow_value("412-268-3259").for(:phone)
      should allow_value("412.268.3259").for(:phone)
      should allow_value("(412) 268-3259").for(:phone)
      should_not allow_value("2683259").for(:phone)
      should_not allow_value("14122683259").for(:phone)
      should_not allow_value("4122683259x224").for(:phone)
      should_not allow_value("800-EAT-FOOD").for(:phone)
      should_not allow_value("412/268/3259").for(:phone)
      should_not allow_value("412-2683-259").for(:phone)

  # Assistance-based tests
      # tests for gov_assistance
      should allow_value(true).for(:gov_assistance)
      should allow_value(false).for(:gov_assistance)

      should_not allow_value(150).for(:gov_assistance)
      should_not allow_value("Yes").for(:gov_assistance)

      # tests for is_employed
      should allow_value(true).for(:is_employed)
      should allow_value(false).for(:is_employed)

      should_not allow_value(30000).for(:is_employed)
      should_not allow_value("Movie theater usher").for(:is_employed)

      # tests for is_veteran
      should allow_value(true).for(:is_veteran)
      should allow_value(false).for(:is_veteran)

      should_not allow_value(nil).for(:is_veteran)
      should_not allow_value("Marines").for(:is_veteran)

  # Establish context
  # Testing other methods with a context
  context "Creating seven clients" do
    setup do 
      @dan = FactoryGirl.create(:client)
      @barney = FactoryGirl.create(:client, :last_name => "Saha", :first_name => "Barney", :active => false, :ethnicity => "Indian" )
      @ryan = FactoryGirl.create(:client, :last_name => "Black", :first_name => "Ryan", :phone => "412-867-5309", :ethnicity => "White", :gov_assistance => true )
      @joe = FactoryGirl.create(:client, :last_name => "Oak", :first_name => "Joseph", :ethnicity => "Asian", :is_employed => false )
      @mary = FactoryGirl.create(:client, :last_name => "Clute", :first_name => "Mary", :gender => "female", :ethnicity => "White" )
      @jon = FactoryGirl.create(:client, :last_name => "Carreon", :first_name => "Jon", :is_veteran => true )
      @meg = FactoryGirl.create(:client, :last_name => "Smith", :first_name => "Megan", :ethnicity => "White", :gender => "female", :is_employed => false)
    end

    # and provide a teardown method as well
    teardown do
      @dan.destroy
      @barney.destroy
      @ryan.destroy
      @joe.destroy
      @mary.destroy
      @jon.destroy
      @meg.destroy
    end

    # test one of each factory
    should "show that all factories are properly created" do
      assert_equal "Tabrizi", @dan.last_name
      assert @ryan.active
      assert @joe.active
      assert_equal "Mary", @mary.first_name
      assert @jon.active
      assert @meg.active
      deny @barney.active
    end

    # test the callback is working 'reformat_phone'
    should "shows that Ryan's phone is stripped of non-digits" do
      assert_equal "4128675309", @ryan.phone
    end

    # test the scope 'alphabetical'
    should "shows that there are seven clients in in alphabetical order" do
      assert_equal ["Black", "Carreon", "Clute", "Oak", "Saha", "Smith", "Tabrizi"], Client.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'active'
    should "shows that there are six active clients" do
      assert_equal 2, Client.active.size
      assert_equal ["Black", "Carreon", "Clute", "Oak", "Smith", "Tabrizi"], Client.active.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'inactive'
    should "shows that there is one inactive client" do
      assert_equal 1, Client.inactive.size
      assert_equal ["Saha"], Client.inactive.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'receiving_gov_assistance'
    should "shows that there is one client receiving government assistance" do
      assert_equal 1, Client.receiving_gov_assistance.size
      assert_equal ["Black"], Client.receiving_gov_assistance.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'not_receiving_gov_assistance'
    should "shows that there are six clients not receiving government assistance" do
      assert_equal 6, Client.not_receiving_gov_assistance.size
      assert_equal ["Carreon", "Clute", "Oak", "Saha", "Smith", "Tabrizi"], Client.not_receiving_gov_assistance.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'male'
    should "shows that there are five male clients" do
      assert_equal 6, Client.male.size
      assert_equal ["Black", "Carreon", "Oak", "Saha", "Tabrizi"], Client.male.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'female'
    should "shows that there are two female clients" do
      assert_equal 2, Client.female.size
      assert_equal ["Clute", "Smith"], Client.female.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'employed'
    should "shows that there are five employed clients" do
      assert_equal 5, Client.employed.size
      assert_equal ["Black", "Carreon", "Clute", "Saha", "Tabrizi"], Client.employed.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'unemployed'
    should "shows that there are two unemployed clients" do
      assert_equal 2, Client.unemployed.size
      assert_equal ["Oak", "Smith"], Client.unemployed.alphabetical.map{|s| s.last_name}
    end

    # test the scope 'veteran'
    should "shows that there is one employed clients" do
      assert_equal 1, Client.veteran.size
      assert_equal ["Carreon"], Client.veteran.alphabetical.map{|s| s.last_name}
    end

    # test the method 'name' #DONE
    should "shows name as last, first name" do
      assert_equal "Tabrizi, Dan", @dan.name
    end   

    # test the method 'proper_name' #DONE
    should "shows proper name as first and last name" do
      assert_equal "Dan Tabrizi", @dan.proper_name
    end 

  end
end

Thank you very much for your help. If there are any other files that are necessary to determine the issue, please let me know. Sorry if this is very simple or too vague, I’m new to Rails development and I am using the terminology to the best of my knowledge

EDIT

Below is my client factory as it currently stands, in app_name/test/factories.rb

FactoryGirl.define do
  factory :client do
    last_name "Tabrizi"
    first_name "Dan"
    gender "male"
    ethnicity "Hispanic"
    marital_status "single"
    address "123 Example Lane"
    city "Anytown"
    state "PA"
    zip "12345"
    phone { rand(10 ** 10).to_s.rjust(10,'0') }
    gov_assistance false
    is_employed true
    is_veteran false
    active true
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:44:36+00:00Added an answer on June 8, 2026 at 8:44 pm

    Hope you perform tests against separate test database, so it is safe to do:

    teardown do
      Client.destroy_all
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app that is using Devise, with a User model, no
I am creating a rails app and have used this code in one of
When creating new Rails app I get this error <internal:lib/rubygems/custom_require>:29:in `require': libcrypto.so.0.9.8: cannot open
I'm trying to run a rails app using nginx and thin. I have configured
I'm creating a Rails app, and I have a model called User . In
In my Rails app, when creating a business I have a form that has
I'm planning on creating an app (Rails) that will have a very large collection
Consider this scenario. We have an internal Rails 2 app that connects to a
I am creating a new rails app as a learning project. I have already
I'm working on creating a Rails app that allows users to set availability Sunday

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.