<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>&lt;p&gt;This RSpec tutorial is intended to be an introduction to RSpec for those with little testing experience or Test::Unit testing experience.  All RSpec examples in these tutorials are based on version 1.0 syntax.&lt;/p&gt;

&lt;p&gt;If I intend to build my project from the start using RSpec, I can opt to use the rspec_scaffold instead of scaffold_resource (which will take over scaffold in Rails 2.0).&lt;/p&gt;

&lt;pre lang="ruby"&gt;fr@ivolo.us$  ./script/generate rspec_scaffold post title:string body:text author:integer created_at:datetime updated_at:datetime

      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/posts
      create  spec/controllers/
      create  spec/models/
      create  spec/helpers/
      create  spec/fixtures/
      create  spec/views/posts
      create  spec/controllers/posts_controller_spec.rb
      create  app/controllers/posts_controller.rb
      create  spec/helpers/posts_helper_spec.rb
      create  app/helpers/posts_helper.rb
      create  app/views/posts/index.rhtml
      create  app/views/posts/show.rhtml
      create  app/views/posts/new.rhtml
      create  app/views/posts/edit.rhtml
      create  app/models/post.rb
      create  spec/fixtures/posts.yml
      create  spec/models/post_spec.rb
      create  spec/views/posts/edit.rhtml_spec.rb
      create  spec/views/posts/index.rhtml_spec.rb
      create  spec/views/posts/new.rhtml_spec.rb
      create  spec/views/posts/show.rhtml_spec.rb
      create  db/migrate
      create  db/migrate/001_create_posts.rb
       route  map.resources :posts
&lt;/pre&gt;

&lt;p&gt;As you can see it generated the model, controller, helper, views and all corresponding specs for me.  I'll open spec/models/post_spec.rb&lt;/p&gt;

&lt;pre lang="ruby"&gt;# this is called a context (or a describe block)
require File.dirname(__FILE__) + '/../spec_helper'

describe Post do
  before(:each) do
    @post = Post.new
  end

  # this is called an example:
  it "should be valid" do
    @post.should be_valid
  end
end
&lt;/pre&gt;

&lt;p&gt;There's something implicit in this spec that's worth pointing out.  All specs have a behaviour_type which determines what helper methods are available to you as you build your specs.  If we were to specify this explicitly it would change one line:&lt;/p&gt;

&lt;pre lang="ruby"&gt;describe Post, "a useful comment", :behaviour_type =&gt; :model do
&lt;/pre&gt;

&lt;p&gt;Right now, this test is pretty useless because the model doesn't do anything, so I'll make it do something really simple:&lt;/p&gt;

&lt;pre lang="ruby"&gt;class Post &lt; ActiveRecord::Base
  validates_presence_of :title, :body
  validates_uniqueness_of :title
end
&lt;/pre&gt;

&lt;p&gt;Some people consider testing validations to be useless because I can be pretty sure that Rails has already done so.  Considering validations are probably the lowest level of architecture in my models, it's a good idea just to verify that they're doing what I think they're doing.&lt;/p&gt;

&lt;pre lang="ruby"&gt;describe Post do
  before(:each) do
    @post = Post.new(valid_post_hash) # grabs the hash below
  end

  it "should be valid" do
    @post.should be_valid
  end

  it "should not be valid without a title" do
    @post.title = ''
    @post.should_not be_valid
  end

  it "should not be valid without a body" do
    @post.body = ''
    @post.should_not be_valid
  end

  def valid_post_hash
    {:title =&gt; 'test', :body =&gt; 'test body'}
  end
end
&lt;/pre&gt;

&lt;p&gt;RSpec is not as readable as many would like it to be, but hopefully it's readable enough that I don't need to explain the above.  I will mention some optional items that could be put in there:&lt;/p&gt;

&lt;pre lang="ruby"&gt;  before(:all) do
    # this will be run once before all examples in this describe block
  end

  before do
    # same as before(:each)
  end

  after(:each) do
    # this will be run once after each example
    @post.destroy unless @post.new_record?
  end

  after(:all) do
    # this will be run once after all examples in this describe block
    # Useful tasks to put in here are:
    Post.destroy_all
  end
&lt;/pre&gt;
</body>
  <classification nil="true"></classification>
  <created-at type="datetime">2007-10-04T00:05:05Z</created-at>
  <id type="integer">5</id>
  <permalink>rspec-tutorial-part-2-a-simple-test</permalink>
  <title>RSpec Tutorial Part 2 - A Simple Test</title>
  <updated-at type="datetime">2007-10-10T02:19:40Z</updated-at>
  <user-id type="integer">1</user-id>
</post>
