控制器规格+布料器

时间:2012-03-18 09:29:01

标签: ruby-on-rails ruby-on-rails-3.1 rspec2 draper

我正在使用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 ...>]>

有什么建议吗?

1 个答案:

答案 0 :(得分:-1)

为什么只测试你的指定中是否有这个Decorator定义?

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