3

I'm using rspec and draper gem https://github.com/jcasimir/draper

In my controller it's a simple action show

def show
  @category = Category.find(params[:id])
  @products = ProductDecorator.decorate(@category.products)
end

and test

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

In project all works fine and products are displayed normally, but in test I get such error

Failure/Error: it { assigns(:products).should eq [@product1, @product2] }

   expected: [#<Product ... >, #<Product ...>]
        got: nil

   (compared using ==)

also if I replace ProductDecorator.decorate(@category.products) with just @category.products - no errors

if I inspect @products

def show
  @category = Category.find(params[:id])
  @products = ProductDecorator.decorate(@category.products)
  puts @products.inspect
end

got

#<DecoratedEnumerableProxy of ProductDecorator for [#<Product ...>, #<Product ...>]>

Any suggestions?

Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70
Ivan Linko
  • 966
  • 7
  • 15

1 Answers1

-1

Why just test you have this Decorator define in your assigns ?

it { assigns(:products).should eq(ProductDecorator.decorate([@product1, @product2] }))
shingara
  • 46,608
  • 11
  • 99
  • 105
  • Yes, I have tried this, but what is a difference to compare products array or 'DecoratedEnumerableProxy of ProductDecorator' with nil? – Ivan Linko Mar 22 '12 at 09:01