TL;DR -> Remove the space after `expect`.

The space in `expect (FactoryGirl.build(:product)).to` causes the `.to` to be 
called on the factory, not the RSpec expectation. This is a Ruby parsing 
“Quirk”. So you need to remove that space so it reads `expect( 
FactoryGirl.build(:product) ).to`.  

Jon Rowe
---------------------------
[email protected]
jonrowe.co.uk


On Friday, 12 December 2014 at 00:25, Roelof Wobben wrote:

> and another problem.  
>  
> I want to try to make it work with factories,
>  
> So I made this factory.  
>  
> spec/factories/products.rb :  
>  
> FactoryGirl.define do  
>      
>     factory :product do  
>         description "This is the first book"  
>         image_url "test.jpg"
>         price 1.00  
>         sequence (:title) { |n| "book#{n}" }
>     end
>      
> end
>  
> spec file :  
>  
> require 'spec_helper'
>      
>     describe Product do
>          
>      
>         it "is valid with a productname, description and a image_url " do  
>             expect (FactoryGirl.build(:product)).to be_valid
>         end
>  
>     end
>  
> but when I ran rspec now I see this message :  
>  
> Failure/Error: expect (FactoryGirl.build(:product)).to be_valid               
>                                                                               
>          
>      NoMethodError:                                                           
>                                                                               
>               
>        undefined method `to' for #<Product:0x007f5fcb1a9398>          
>  
> Roelof
>  
>  
>  
> Op donderdag 11 december 2014 09:43:17 UTC+1 schreef Roelof Wobben:
> > Thanks,  
> >  
> > I noticed something wierd in the ecommerce book.  
> >  
> > On the model there is first stated :  validates :title, :description, 
> > :image_url, presence: true
> > which means that the image_url must be filled in .
> >  
> > Later is stated : validates :image_url, allow_blank: true,
> > Which  means the image_url can be empty.  
> >  
> > Do I think right  ? and wierd minitest is not finding this  
> >  
> > Roelof
> >  
> >  
> > Op donderdag 11 december 2014 07:15:16 UTC+1 schreef Myron Marston:
> > > On Tuesday, December 9, 2014 11:15:07 PM UTC-8, Roelof Wobben wrote:
> > > > Here my repo : https://github.com/roelof1967/commerce-try
> > > >  
> > > > Roelof
> > > >  
> > > > Op woensdag 10 december 2014 07:42:47 UTC+1 schreef Roelof Wobben:
> > > > > I did also rails g rspec:install otherwise rspec do not give output.  
> > > > >  
> > > > > Roelof
> > > > >  
> > > > >  
> > > > > Op woensdag 10 december 2014 00:23:21 UTC+1 schreef Carlos Figueiredo:
> > > > > > Minitest is the default test suite on Rails.
> > > > > >  
> > > > > > Did you just installed rspec-rails gem, or did you also run `rails 
> > > > > > generate rspec:install` ?
> > > > > >  
> > > > > > Because `rails generate rspec:install` configures you env to run 
> > > > > > rspec.
> > > > > >  
> > > > > > Carlos
> > > > > >  
> > > > > > On Tue, Dec 9, 2014 at 7:12 PM, Roelof Wobben <[email protected]> 
> > > > > > wrote:
> > > > > > >  
> > > > > > > I use the rspec rails gem .
> > > > > > >  
> > > > > > > I found this in the gem file :  
> > > > > > >  
> > > > > > >  gem "rspec-rails", "~> 2.14.0"
> > > > > > >  
> > > > > > > If you want , I can upload this project to my personal github 
> > > > > > > page.  
> > > > > > >  
> > > > > > > How can I print out the product.errors ?
> > > > > > >  
> > > > > > > Roelof
> > > > > > >  
> > > > > > > Op dinsdag 9 december 2014 21:53:35 UTC+1 schreef Myron Marston:
> > > > > > > > On Tuesday, December 9, 2014 11:05:26 AM UTC-8, Roelof Wobben 
> > > > > > > > wrote:
> > > > > > > > > I will  cut the error message in two.  
> > > > > > > > >  
> > > > > > > > > the minitest error message :  
> > > > > > > > >  
> > > > > > > > > Warning: you should require 'minitest/autorun' instead.       
> > > > > > > > >                                                               
> > > > > > > > >                                               
> > > > > > > > > Warning: or add 'gem "minitest"' before 'require 
> > > > > > > > > "minitest/autorun"'      
> > > > > > > > >  
> > > > > > > > > and the Rspec error message :  
> > > > > > > > >  
> > > > > > > > > Failures:                                                     
> > > > > > > > >                                                               
> > > > > > > > >                                               
> > > > > > > > >                                                               
> > > > > > > > >                                                               
> > > > > > > > >                                               
> > > > > > > > >   1) Product is valid with a productname, description and a 
> > > > > > > > > image_url                                                     
> > > > > > > > >                                                 
> > > > > > > > >      Failure/Error: expect(product).to be_valid               
> > > > > > > > >                                                               
> > > > > > > > >                                               
> > > > > > > > >        expected valid? to return true, got false              
> > > > > > > > >                                                               
> > > > > > > > >                                               
> > > > > > > > >      # ./spec/model/product_spec.rb:10:in `block (2 levels) 
> > > > > > > > > in <top (required)>'     
> > > > > > > > >  
> > > > > > > > > Roelof
> > > > > > > >  
> > > > > > > > The expectation failure is telling you that `product.valid?` 
> > > > > > > > did not return true as expected.   It's impossible for us to 
> > > > > > > > say what specifically is making it invalid.  You'll have to 
> > > > > > > > check `product.errors` to see what the validation errors are.  
> > > > > > > > It looks like your spec is running without rspec-rails loaded 
> > > > > > > > (since `be_valid` isn't providing the errors -- the default 
> > > > > > > > `be_valid` matcher in rspec-expectations just checks `valid?` 
> > > > > > > > but doesn't know to look for `errors`).  If you load 
> > > > > > > > `rspec-rails`, an improved `be_valid` matcher is available that 
> > > > > > > > will include the validation errors in the failure message:
> > > > > > > >  
> > > > > > > > https://github.com/rspec/rspec-rails/blob/v3.1.0/lib/rspec/rails/matchers/be_valid.rb
> > > > > > > >  
> > > > > > > > If you use that, it should pinpoint what the validation error 
> > > > > > > > is, and then you can fix it.
> > > > > > > >  
> > > > > > > > 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/a68be083-ab07-437b-b3ec-4ddb9a978b5d%40googlegroups.com
> > > > > > >  
> > > > > > > (https://groups.google.com/d/msgid/rspec/a68be083-ab07-437b-b3ec-4ddb9a978b5d%40googlegroups.com?utm_medium=email&utm_source=footer).
> > > > > > >  
> > > > > > > For more options, visit https://groups.google.com/d/optout.
> > > > > >  
> > >  
> > > OK, I've taken a look at your repository and discovered a few things:
> > >  The minitest warning is triggered by shoulda-matchers.  If you read the 
> > > changelog[1], you'll notice that 2.6.0 fixes this, so you should upgrade.
> > > You're on an old version of RSpec, too -- please upgrade to RSpec 3 [2]
> > > The rspec-rails `be_valid` matcher isn't being used in your case because 
> > > your spec file is in the `spec/model` directory, not the `spec/models` 
> > > directory.  These sorts of subtleties are why we changed things in RSpec 
> > > 3 so that spec types aren't auto-inferred by the directory unless you opt 
> > > in [3], and I'd encourage you to explicitly tag each spec with `:type` 
> > > metadata (e.g. `:type => :model`).
> > > Once you tag the example group or move the spec file to `spec/models`, 
> > > you'll get the validation errors in the failure message and from there 
> > > you should be able to address tehm.
> > >  
> > > HTH,
> > > Myron
> > >  
> > > [1] https://github.com/thoughtbot/shoulda-matchers/blob/master/NEWS.md
> > > [2] http://rspec.github.io/upgrading-from-rspec-2/
> > > [3] 
> > > http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#filetype_inference_disabled_by_default
> > >  
> >  
> >  
>  
>  
>  
> --  
> 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] 
> (mailto:[email protected]).
> To post to this group, send email to [email protected] 
> (mailto:[email protected]).
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rspec/23ba00a6-c4ea-46d2-80e4-0d4bf7a23d18%40googlegroups.com
>  
> (https://groups.google.com/d/msgid/rspec/23ba00a6-c4ea-46d2-80e4-0d4bf7a23d18%40googlegroups.com?utm_medium=email&utm_source=footer).
> For more options, visit https://groups.google.com/d/optout.

-- 
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/14B9560DB3D045ADAD03BB8140EE2E2A%40jonrowe.co.uk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to