May 29, 2008
Generic-user-small Michael McDe... 5 posts

Topic: Agile Web Development with Rails, 3rd Edition / RSpec ?

I would love RSPEC inclusion, but it seems like Shoulda would be a good fit since it extends Test::Unit. Is test/spec under development? I thought it was halted as the author worked on a new bdd testing package.

 
Mar 7, 2008
Generic-user-small Michael McDe... 5 posts

Topic: Advanced Rails Recipes / Should Recipe and Postgres

Thanks I’ll give it a whirl.

 
Mar 6, 2008
Generic-user-small Michael McDe... 5 posts

Topic: Advanced Rails Recipes / Should Recipe and Postgres

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                                   
 
Mar 6, 2008
Generic-user-small Michael McDe... 5 posts

Topic: Advanced Rails Recipes / Should Recipe and Postgres

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

 
Mar 5, 2008
Generic-user-small Michael McDe... 5 posts

Topic: Advanced Rails Recipes / Should Recipe and Postgres

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!

5 posts