On Tuesday, December 9, 2014 8:14:34 AM UTC-8, Roelof Wobben wrote:
>
> Hello, 
>
> I have this model : 
>
> class Product < ActiveRecord::Base
>     validates :title, :description, :image_url, presence: true
>     validates :price, numericality: {greater_than_or_equal_to: 0.01}
>     validates :title, uniqueness: true
>     validates :image_url, allow_blank: true,
>     format: { with: %r{\.(gif|jpg|png)\Z}i,
>     message: 'must be a URL for GIF, JPG or PNG image.'
>   }
> end
>
> So I have made this test 
>
> require 'spec_helper'
>     
>     describe Product do
>     
>         it "is valid with a productname, description,price and a 
> image_url" do 
>          product = Product.new( 
>                 title = "Book 1" ,
>                 description = "This is the first book" ,
>                 price = "1.00")  
>         expect(product).to be_valid
>     end
> end
>
> But  as soon as I do rspec I see this output: 
>
>  
> MiniTest::Unit::TestCase is now Minitest::Test. From 
> /home/codio/.rbenv/versions/2.1.5/lib/ruby/2.1.0/test/unit/testcase.rb:8:in 
> `<module:Unit>'                         
>
>                                                                               
>                                                                               
>              
>
> Product                                                                       
>                                                                               
>              
>
>   is valid with a productname, description,price and a image_url (FAILED - 1)
>                                                                               
>               
>
>                                                                               
>                                                                               
>              
>
> Failures:                                                                     
>                                                                               
>              
>
>                                                                               
>                                                                               
>              
>
>   1) Product is valid with a productname, description,price and a image_url   
>                                                                               
>              
>      Failure/Error: product = Product.new(
>                                                                               
>                                                  
>      ArgumentError
> :                                                                             
>                                                                          
>        wrong number of arguments (3 for 0..2)
>                                                                               
>                                               
>
>      # ./spec/model/product_spec.rb:6:in `block (2 levels) in <top 
> (required)>'
>             
>
>
> Where did i took the wrong turn. 
>
> Roelof
>

 Change this:

Product.new( 
                title = "Book 1" ,
                description = "This is the first book" ,
                price = "1.00")

to:

Product.new( 
                title: "Book 1" ,
                description: "This is the first book" ,
                price: "1.00")

In the first case, you are passing 3 positional arguments, and assigning 
them to local variables `title`, `description` and `price`.  In the second, 
you are passing a single positional argument, which is a hash containing 
`:title`, `:description` and `:price`.  ActiveRecord only supports the 
latter.

HTH,
Myron

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/65264b4b-27ff-489e-8f41-b7ac6cdcfa39%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to