3
我使用的RSpec和布店寶石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 ==)
另外,如果我用替換ProductDecorator.decorate(@ category.products)剛剛@ 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 ...>]>
任何建議?
是的,我試過這個,但有什麼區別比較產品數組或'ProductDecorator的DecoratedEnumerableProxy'與零? – 2012-03-22 09:01:46