Kamis, 03 Maret 2011

Rspec simple test

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.
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).
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
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
# 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
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:
describe Post, "a useful comment", :behaviour_type => :model do
Right now, this test is pretty useless because the model doesn't do anything, so I'll make it do something really simple:
class Post < ActiveRecord::Base
  validates_presence_of :title, :body
  validates_uniqueness_of :title
end
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.
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 => 'test', :body => 'test body'}
  end
end
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:
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

Tidak ada komentar:

Posting Komentar