我正在使用 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 ...>]>
有什么建议么?