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