我使用的是rspec和draper gem https://github.com/jcasimir/draper
在我的控制器中,这是一个简单的动作展示
def show
@category = Category.find(params[:id])
@products = ProductDecorator.decorate(@category.products)
end和测试
describe "#show" do
before { @category = Factory :category }
before do
@product1 = Factory :product, category: @category
@product2 = Factory :product, category: @category
end
before { get :show, id: @category.id }
it { should respond_with :success }
it { assigns(:products).should eq [@product1, @product2] }
end在项目中,所有工作正常,产品显示正常,但在测试中我得到这样的错误
Failure/Error: it { assigns(:products).should eq [@product1, @product2] }
expected: [#<Product ... >, #<Product ...>]
got: nil
(compared using ==)同样,如果我只用@category.products替换ProductDecorator.decorate(@category.products) -没有错误
如果我检查@products
def show
@category = Category.find(params[:id])
@products = ProductDecorator.decorate(@category.products)
puts @products.inspect
end得到
#<DecoratedEnumerableProxy of ProductDecorator for [#<Product ...>, #<Product ...>]>有什么建议吗?
发布于 2012-03-20 20:52:07
为什么只是测试你在作业中定义了这个装饰者?
it { assigns(:products).should eq(ProductDecorator.decorate([@product1, @product2] }))https://stackoverflow.com/questions/9757271
复制相似问题