ruby on rails - how to include module for MiniTest -
i have class company
include growthrate
.
models/company.rb
class company < activerecord::base include growthrate end
in growth_rate.rb
, add methods array
.
models/company/growth_rate.rb
module company::growthrate extend activesupport::concern end module company::growthrate::array def growth_rate # calculate growth rate end end class array include company::growthrate::array end
and want test method of array minitest.
test/models/company/growth_rate_test.rb
require 'test_helper' class companytest < activesupport::testcase include company::growthrate test 'test adjusted_growth_rate' array = [1, 0.9] array.stub :growth_rate, 1 # assert_equal end end end
but test ends name error.
nameerror: undefined method `growth_rate' `company::growthrate::array'
how can include method minitest?
test/test_helper.rb
env['rails_env'] ||= 'test' require file.expand_path('../../config/environment', __file__) require 'rails/test_help' require 'minitest/mock' class activesupport::testcase # setup fixtures in test/fixtures/*.yml tests in alphabetical order. fixtures :all end
i think have move models/company/growth_rate.rb
app/model/concerns folder filename = 'growth_rate.rb'
then don't use company class prevent conflict class name
module growthrate extend activesupport::concern # ... end
now can include company model
then create array.rb file in config/initializers folder contains
class array def growth_rate # calculate growth rate end end
this file loaded once rails, it's adding custom method array class if want to
now can remove include company::growthrate
test/models/company/growth_rate_test.rb
Comments
Post a Comment