Generic-user-small Michael McDe... 4 posts

Hello,
I just tried the Shoulda Recipe (51) with Rails 2.0.2 and Postgres (8.1 on windows, 8.2 on Ubuntu) and I’m getting an error when I run the test.
I’m using a new rails application and the dev and test databases works (I did a migration and ran rake db:test:prepare to make sure).
I’ve tried this with both the version of shoulda in the recipe and the current version. And the only file I’ve created is the perscribed test/unit/event_test.rb

Any ideas?

C:\RailsDev\shoulda_test>rake test
(in C:/RailsDev/shoulda_test)
C:/ruby/bin/ruby -Ilib;test “C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/
rake_test_loader.rb” “test/unit/event_test.rb”
./test/unit/event_test.rb:1: uninitialized constant Test (NameError) from C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb:5:in `load’ from C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb:5 from C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb:5:in `each’ from C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb:5
C:/ruby/bin/ruby -Ilib;test “C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb”
C:/ruby/bin/ruby -Ilib;test “C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb”
Errors running test:units!

 
Mike-120_small Mike Clark Administrator 26 posts

Hi Michael -

What does your event_test.rb file look like?

Mike

 
Generic-user-small Michael McDe... 4 posts

Mike, Thanks for the reply.

Here is all I have


class EventTest < Test::Unit::TestCase
  should_require_attributes :name, :description,:image_location,
                            :starts_at, :location, :capacity
#  should_require_unique_attributes :name

#  should_only_allow_numeric_values_for :price

#  should_not_allow_values_for :price, -1.0, :message => /must be greater than or equal to 0/

#  should_have_many :attendees, :through => :registrations

end

 
Mike-120_small Mike Clark Administrator 26 posts

That looks fine. The error seems to indicate that the test/unit library can’t be found. Can you run regular units, without using Shoulda at all?

 
Generic-user-small Michael McDe... 4 posts

Mike,
It didn’t run a regular test so I checked out AWDR2 to see what I was doing wrong in the regular test. Then I noticed:


require File.dirname(__FILE__) + '/../test_helper'
class EventTest < Test::Unit::TestCase
#code
end

the require was missing and that was it. That might be worth mentioning in the recipe.

I went on to try more of the recipe. I coded up the shoulda spec and the model, but now I’m getting additional problems.


require File.dirname(__FILE__) + '/../test_helper'

class EventTest < Test::Unit::TestCase
  should_require_attributes :name, :description,:image_location,
                            :starts_at, :location, :capacity
  should_require_unique_attributes :name
  should_only_allow_numeric_values_for :price
  should_not_allow_values_for :price, -1.0, :message => /must be greater than or equal to 0/
  should_have_many :attendees, :through => :registrations
end

class Event < ActiveRecord::Base
  validates_presence_of :name, :description, :image_location, 
                        :starts_at, :location, :capacity
  validates_uniqueness_of :name

  validates_numericality_of :price , :greater_than_or_equal_to => 0.0

  has_many :registrations
  has_many :attendees, :through => :registrations,
                       :source => :user
end

It seems, however, that there is a requirement that some objects already exist to test those. I’ve tried it with bot 3.0.4 of shoulda and the current version and get the same results.


C:\RailsDev\shoulda_test>ruby test/unit/event_test.rb                           
Loaded suite test/unit/event_test                                               
Started                                                                         
.FF......F                                                                      
Finished in 3.985 seconds.                                                      

  1) Failure:                                                                   
test: should not allow price to be set to "-1.0". (EventTest)                   
    [C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/active_record_
helpers.rb:127:in `__bind_1204836328_984000'                                    
     C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/context.rb:98:
in `call'                                                                       
     C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/context.rb:98:
in `test: should not allow price to be set to "-1.0". ']:                       
Can't find first Event.                                                         
<nil> is not true.                                                              

  2) Failure:                                                                   
test: should only allow numeric values for price. (EventTest)                   
    [C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/active_record_
helpers.rb:255:in `__bind_1204836329_390000'                                    
     C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/context.rb:98:
in `call'                                                                       
     C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/context.rb:98:
in `test: should only allow numeric values for price. ']:                       
Can't find first Event.                                                         
<nil> is not true.                                                              

  3) Failure:                                                                   
test: should require unique value for name. (EventTest)                         
    [C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/active_record_
helpers.rb:64:in `__bind_1204836329_562000'                                     
     C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/context.rb:98:
in `call'                                                                       
     C:/RailsDev/shoulda_test/vendor/plugins/shoulda4/lib/shoulda/context.rb:98:
in `test: should require unique value for name. ']:                             
Can't find first Event.                                                         
<nil> is not true.                                                              

10 tests, 25 assertions, 3 failures, 0 errors                                   
 
Mike-120_small Mike Clark Administrator 26 posts

Ah! You’ll need the events.yml and registrations.yml test fixture files. I’ll update the recipe. You can grab those files from the code bundle.

Thanks!

 
Generic-user-small Michael McDe... 4 posts

Thanks I’ll give it a whirl.

7 posts, 2 voices