【发布时间】:2016-09-13 00:50:29
【问题描述】:
我有一个Company 的课程include GrowthRate。
models/company.rb
class Company < ActiveRecord::Base
include GrowthRate
end
在growth_rate.rb中,我为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
我想通过MiniTest测试Array的方法。
test/models/company/growth_rate_test.rb
require 'test_helper'
class CompanyTest < ActiveSupport::TestCase
include Company::GrowthRate
test 'test for adjusted_growth_rate' do
array = [1, 0.9]
Array.stub :growth_rate, 1 do
# assert_equal
end
end
end
但测试以名称错误结束。
NameError: undefined method `growth_rate' for `Company::GrowthRate::Array'
如何包含 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 all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
end
【问题讨论】:
-
您在测试
growth_rate方法吗?如果是,为什么会被存根? -
对不起,测试是针对
def adjusted_growth_rate,所以我想存根growth_rate。我改变了问题。 -
您是否尝试过包括:
require 'test_helper'?还有,test/test_helper.rb 的内容是什么? -
是的,我需要
test_helper,并在我的问题中添加了它的内容。