[rspec-users] Comparing new models

David Chelimsky dchelimsky at gmail.com
Mon Apr 7 10:51:06 EDT 2008


On Sun, Apr 6, 2008 at 4:33 PM, Rare <ryan at thinkrefresh.com> wrote:
> Hi all,
>
>  I am trying to test that a view file in Rails is being sent a new
>  empty model (Product.new) in my rspec test, below is my code. I've
>  tried comparing them with ==, ===, equal, eql none work. I just get an
>  error (see below code). I guess it's the condition tester (e.g. ==)
>  that is incorrect, can anyone help?
>
>  Code:
>
>  it "should assign the new product for the view" do
>   do_get
>   assigns[:product].should == Product.new
>  end

Try this in irb:

Product.new == Product.new
=> false

This is why what you have is failing. What will work is something like this:

it "should assign the new product for the view" do
 product = Object.new
 Product.should_receive(:new).and_return(product)
 do_get
 assigns[:product].should equal(product)
end

You can use product = Product.new for the first line, but I'm pretty
sure Object.new is cheaper.

HTH,
David

>
>  Error:
>
>  'ProductsController handling GET /products/new should assign the new
>  product for the view' FAILED
>   expected: #<Product id: nil, name: nil, permalink: nil, description:
>  nil, price: nil, is_enabled: nil, created_at: nil, updated_at: nil,
>  manufacturer_id: nil>,
>   got:      #<Product id: nil, name: nil, permalink: nil, description:
>  nil, price: nil, is_enabled: nil, created_at: nil, updated_at: nil,
>  manufacturer_id: nil> (using ==)
>  ./spec/controllers/products_controller_spec.rb:118:
>  _______________________________________________
>  rspec-users mailing list
>  rspec-users at rubyforge.org
>  http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list