shoulda macros with rspec2 beta 5 and rails3 beta2
Asked Answered
D

3

5

I've setup Rspec2 beta5 and shoulda as following to use shoulda macros inside rspec model tests.

=================

Update 2011-Feb-18

Now we can use shoulda-matchers out of the box.

Just add gem shoulda-matchers in you Gemfile and nothing else in spec_helper or any hack. It just runs.

=================

Gemfile

group :test do
  gem "rspec", ">= 2.0.0.beta.4"
  gem "rspec-rails", ">= 2.0.0.beta.4"
  gem 'shoulda',          :git => 'git://github.com/bmaddy/
shoulda.git'
  gem "faker"
  gem "machinist"
  gem "pickle",           :git => 'git://github.com/codegram/
pickle.git'
  gem 'capybara',         :git => 'git://github.com/jnicklas/
capybara.git'
  gem 'database_cleaner', :git => 'git://github.com/bmabey/
database_cleaner.git'
  gem 'cucumber-rails',   :git => 'git://github.com/aslakhellesoy/
cucumber-rails.git'
end

spec_helper.rb

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

require 'shoulda'

Rspec.configure do |config|

spec/models/outlet_spec.rb

require 'spec_helper'

describe Outlet do
  it { should validate_presence_of(:name) }
end

And when I run the spec, I get the following error.

[~/rails_apps/rails3_apps/automation (master)⚡] ➔ spec spec/models/
outlet_spec.rb
DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead.
(called from join at /home/millisami/.rvm/gems/ruby-1.9.1-p378%rails3/
bundler/gems/shoulda-87e75311f83548760114cd4188afa4f83fecdc22-master/
lib/shoulda/autoload_macros.rb:40)
F

1) Outlet
    Failure/Error: it { should validate_presence_of(:name) }
    undefined method `validate_presence_of' for
#<Rspec::Core::ExampleGroup::Nested_1:0xc4dc138 @__memoized={}>
    # ./spec/models/outlet_spec.rb:4:in `block (2 levels) in <top
(required)>'

Finished in 0.0399 seconds
1 example, 1 failures
[~/rails_apps/rails3_apps/automation (master)⚡] ➔

Why the "undefined method" ?? Is the shoulda getting loaded?

Dilator answered 7/4, 2010 at 7:54 Comment(2)
Same thing here. Have you ever found out why?Forester
Nop, I abandoned shoulda and started with rspec-rails.Dilator
P
7

Using RSpec 2.0.0.beta.19

# Gemfile
group :test do
  gem "rspec", ">= 2.0.0.beta.19"
  gem "rspec-rails", ">= 2.0.0.beta.17"
  gem "shoulda"
end

# spec/spec_helper.rb
require 'rspec/rails'
require 'shoulda/integrations/rspec2' # Add this line

# In your specs....
it { should validate_presence_of(:name) }

Running rake spec should now load and run specs including the RSpec 2 matchers.

Particle answered 11/8, 2010 at 7:19 Comment(0)
E
0

Think this has something to do with the new syntax of validations in rails3:

validates :name, :presence => true

Emmie answered 12/4, 2010 at 21:25 Comment(0)
D
0

The method should be validates_presence_of. You missed an 's'

Defoliant answered 7/7, 2010 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.