【发布时间】:2012-04-17 17:33:38
【问题描述】:
我在尝试使用 Draper 进行测试时收到以下错误:
NoMethodError: undefined method 'with_unit' for nil:NilClass
这是我的测试代码:
# spec/decorators/food_decorator_spec.rb
require 'spec_helper'
describe FoodDecorator do
before { ApplicationController.new.set_current_view_context }
FactoryGirl.create(:food)
@food = FoodDecorator.first
specify { @food.with_unit('water').should == "85.56 g" }
end
这是我的装饰器代码:
class FoodDecorator < ApplicationDecorator
decorates :food
def with_unit(nutrient)
model.send(nutrient.to_sym).to_s + " g"
end
end
我的研究表明,ApplicationController.new.set_current_view_context 行应该可以解决此问题,但事实并非如此。有什么想法吗?
【问题讨论】:
标签: ruby-on-rails rspec draper