2012-03-18 69 views
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 ...>]> 

任何建议?

回答

-1

为什么只是测试你有这个装饰定义在你的指定?

it { assigns(:products).should eq(ProductDecorator.decorate([@product1, @product2] })) 
+0

是的,我试过这个,但有什么区别比较产品数组或'ProductDecorator的DecoratedEnumerableProxy'与零? – 2012-03-22 09:01:46