Topic: Rspec Examples/Tutorials


Just a BDD (and testing in general) newb hoping all could post links to any RSpec-related cheat sheets, tutorials, examples, etc. that are floating around the web.

Community-based data aggregation never hurt anyone smile

I'll start with a few that I've been using:


Tutorials: … pec-part-1 … pec-part-i

"Yeah, well, you know, that's just, like, your opinion, man." -El Duderino

Re: Rspec Examples/Tutorials

looks like great stuff.
After I finish with 'Agile web development' book I'll dive into Rspec.

1. Is Rspec limited for unit(model) tests?

and regarding functional(controller) and integration tests(multiple controllers):
2. Should I do it using rails built-in methods and techniques, as described in the Agile book?
3. Should I use a test first approach even for those tests?

Re: Rspec Examples/Tutorials

Rspec and Autotest - step by step for rails newbies.

I decided to wait with the book, this is more interesting..
It took me a few hours to make it work, so here is a step by step guide.
If you think that there are some short-cuts, let me know.

First, lets look at this video to see the end result: … test-rails
The idea is to run your tests automatically, every time you change them.

Here are the steps to make it work:
1. create a rails project:
rails test

2. get into the new folder:
cd test

3. install those plugins: ( … stall.html)
(I don't know why do you need both, I am guessing that the second one is the generator of rspec files)
ruby script/plugin install svn://
ruby script/plugin install svn://

4. install the zentest gem:
sudo gem install ZenTest

5. configure the database.yml file (I got errors before doing it).

6. create a model:
script/generate rspec_model User

7. create rtest file:
script/generate rspec_model User

9. tun this command (it's required, I don't know why):
script/generate rspec

10. run the test with rspec: (you don't have too, but it's nice to know how)
rake spec

10. run the test with autotest (it's available thanks to step 4):
autotest -rails

now, autotest listen to any change you make in your test file,
and it will run your tests in the background.

Now I am going to follow the second tutorial you mentioned when autotest is running.

Re: Rspec Examples/Tutorials

I started following the second tutorial and my test fails when it should not.
this is my test file:
require File.dirname(__FILE__) + '/../spec_helper'

context "A user (in general)" do
  setup do
    @user =

  specify "should be invalid without a username" do
    @user.username = 'someusername'

I created the migration file and migrated the table schema.
I added this line to my model: validates_presence_of :username
but that's what i get when i run rake spec:models --trace

NoMethodError in 'A user (in general) should be invalid without a username'
undefined method `should_not_be_valid' for #<User:0xb6fdbd4c>

Finished in 0.058561 seconds

1 example, 1 failure
rake aborted!
Command ruby -I"/home/oren/rails_projects/test/vendor/plugins/rspec/lib" "/home/oren/rails_projects/test/vendor/plugins/rspec/bin/spec" "spec/models/user_spec.rb" --options "/home/oren/rails_projects/test/config/../spec/spec.opts"  failed
/home/oren/rails_projects/test/vendor/plugins/rspec/lib/spec/rake/spectask.rb:132:in `define'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:823:in `verbose'
/home/oren/rails_projects/test/vendor/plugins/rspec/lib/spec/rake/spectask.rb:106:in `define'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in `invoke'
/usr/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in `run'
/usr/bin/rake:16:in `load'

Re: Rspec Examples/Tutorials

Hi Oren,

Looks like you're almost there, just a syntax error (note spaces after should_not and should):

specify "should be invalid without a username" do
  @user.should_not be_valid # equivalent to !@user.valid?
  @user.username = 'someusername'
  @user.should be_valid # equivalent to @user.valid?

Last edited by zubin8or (2007-08-18 09:26:34)