0

I am using minitest 5.3.3 to test a model in a rails 4.1.1 app using ruby 2.0.0p247. After running the test I get

 Finished in 0.017875s, 167.8317 runs/s, 55.9439 assertions/s.                                                                                                                                                          
 1)     Error:                                                                                                                                                                                       
   AdditionTest#test_class_method:                                                                                                                                                        
  NoMethodError: undefined method `number' for nil:NilClass

here is the test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  fixtures :all
end

The test looks like this

class AdditionTest < ActiveSupport::TestCase
  def test_class_method
    m = Addition.multiply(3, 4) 
    assert_equal 12, m
  end
end

The model looks like this and number is table column or model attribute;

class Addition < ActiveRecord::Base
    def self.multiply(first_id, second_id)
       Adder.new(first_id, second_id).multiply
    end
end

The Adder class:

 class Adder
    def initialize(first_id, second_id)
     @first = first_id
     @second = second_id
    end

     def multiply
       @first_value * @second_value
     end

   private

    def fetch_first_value
       @fetch_db = Addition.where(id: @first)
       @get_first = @fetch_db.first
       @first_value = @get_first.number
    end

     def fetch_second_value
       @fetch_db_2 = Addition.where(id: @second)
       @get_second = @fetch_db_2.first
       @second_value = @get_second.number
    end

 end

The yaml file looks like this:

    one:
      number: 5


   two:
     number: 6
brg
  • 3,915
  • 8
  • 37
  • 66
  • When you say `multiply(3, 4)` are you expecting the assigned IDs to be `3` and `4`? That's probably not the case. Your fixtures have `5` and `6` so the only possible answer is `30`. – tadman May 26 '14 at 20:42
  • Thanks @tadman, I have commented pout the whole content of the yaml file, to take it out of the equation. How I still get the **NoMethodError: undefined method `number' for nil:NilClass** – brg May 26 '14 at 20:47
  • Where does `@get_first` and `@get_second` come from? – Mike Szyndel May 26 '14 at 20:47
  • thanks for helping @micheal, they come from calls to the database like **@fetch_db_2 = Addition.where(id: second_id)** and then **@get_second = @fetch_db_2.first** – brg May 26 '14 at 20:52

1 Answers1

0

Your test is the problem here. You're referencing IDs that don't exist. Try this:

class AdditionTest < ActiveSupport::TestCase
  def test_class_method
    m = Addition.multiply(additions(:one), additions(:two)) 
    assert_equal 30, m
  end
end
tadman
  • 208,517
  • 23
  • 234
  • 262
  • Thanks, I will try this now and come back. – brg May 26 '14 at 21:03
  • Thanks @tadman, how can I get it to work if I don't want to use fixtures. – brg May 26 '14 at 21:22
  • You can create the models right there in the test, then pass those through as arguments. What you're doing here is a little odd, so I'm not sure it's the best design, but that's how it would be given the constraints you've imposed. – tadman May 26 '14 at 22:00