what is wrong with this test

141 views
Skip to first unread message

Roelof Wobben

unread,
Dec 9, 2014, 11:14:34 AM12/9/14
to rs...@googlegroups.com
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


Myron Marston

unread,
Dec 9, 2014, 11:43:24 AM12/9/14
to rs...@googlegroups.com
 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

Roelof Wobben

unread,
Dec 9, 2014, 12:16:38 PM12/9/14
to rs...@googlegroups.com
I made this changes and still it fails.

I also see this message :

Warning: you should require 'minitest/autorun' instead.                                                              
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"' 

Roelof



Op dinsdag 9 december 2014 17:43:24 UTC+1 schreef Myron Marston:

Myron Marston

unread,
Dec 9, 2014, 1:56:02 PM12/9/14
to rs...@googlegroups.com
On Tuesday, December 9, 2014 9:16:38 AM UTC-8, Roelof Wobben wrote:
I made this changes and still it fails.

I also see this message :

Warning: you should require 'minitest/autorun' instead.                                                              
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"' 

Roelof

What is the failure message you get now? (It shouldn't be the same since it won't pass 3 arguments if you make my suggested change).

As for the minitest thing: something in your environment is loading minitest.  Without seeing your code it's impossible to guess what is loading it.  Do you have it in your Gemfile?  Or is there a `require "minitest"` anywhere in your code?

While you can mix and match parts of RSpec with parts of minitest, RSpec's runner (rspec-core) isn't designed to work with minitest's runner (what you get when "minitest" is required rather than "minitest/assertions" or "minitest/mock").

Myron

Roelof Wobben

unread,
Dec 9, 2014, 2:05:26 PM12/9/14
to rs...@googlegroups.com
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



Op dinsdag 9 december 2014 19:56:02 UTC+1 schreef Myron Marston:

Myron Marston

unread,
Dec 9, 2014, 3:53:35 PM12/9/14
to rs...@googlegroups.com
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:


If you use that, it should pinpoint what the validation error is, and then you can fix it.

HTH,
Myron

Roelof Wobben

unread,
Dec 9, 2014, 4:12:18 PM12/9/14
to rs...@googlegroups.com

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:

Carlos Figueiredo

unread,
Dec 9, 2014, 6:23:21 PM12/9/14
to rs...@googlegroups.com
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

--
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 rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/a68be083-ab07-437b-b3ec-4ddb9a978b5d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Roelof Wobben

unread,
Dec 10, 2014, 1:42:47 AM12/10/14
to rs...@googlegroups.com
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:

Roelof Wobben

unread,
Dec 10, 2014, 2:15:07 AM12/10/14
to rs...@googlegroups.com

Roelof

Op woensdag 10 december 2014 07:42:47 UTC+1 schreef Roelof Wobben:

Myron Marston

unread,
Dec 11, 2014, 1:15:16 AM12/11/14
to rs...@googlegroups.com
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

Roelof Wobben

unread,
Dec 11, 2014, 3:43:17 AM12/11/14
to rs...@googlegroups.com
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:

Roelof Wobben

unread,
Dec 11, 2014, 8:25:46 AM12/11/14
to rs...@googlegroups.com
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:

Jon Rowe

unread,
Dec 11, 2014, 3:57:52 PM12/11/14
to rs...@googlegroups.com
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
---------------------------

Reply all
Reply to author
Forward
0 new messages