def_delegators
.
例如,本测试:
it 'has the weekly discount' do
//I also tried using create here
menu = FactoryGirl.build(:menu, weekly_price: 100, price: 200)
meal = FactoryGirl.build(:meal, menu: menu)
price = meal.price
end
1) Order has the weekly discount
Failure/Error: price = meal.price
NoMethodError:
undefined method `price' for nil:NilClass
# ./spec/models/order_spec.rb:17:in `block (2 levels) in <top (required)>'
这就是价格的定义
Meal
class Meal < ApplicationRecord
extend Forwardable
belongs_to :menu
def_delegators :@menu, :price
end
如果我将其更改为:
class Meal < ApplicationRecord
extend Forwardable
belongs_to :menu
def price
menu.price
end
end
测试通过。我在其他地方使用def_委托人,在所有测试中都失败了
NoMethodError