访问次数
我的另一个总结性博客: todototry 大米粒

rails 3 使用 cucumber 和rspec 進行 測試

rails3使用cucumber和rspec进行测试

 
rails new blog -d mysql -T

group :development:test do
    gem 'cucumber'"~> 0.10.2"
    gem 'rspec'"~> 2.4" 
    gem 'rspec-rails''~> 2.5'
    gem 'cucumber-rails''~> 0.4.1'
    gem 'capybara''~> 0.4.1.2'
    gem 'database_cleaner''~> 0.6.6'
end

depot$rails generate rspec:install
depot$rails generate cucumber:install


bash

rails blog
sudo rake gems:install RAILS_ENV=test
script/generate cucumber
cucumber features -n
script/generate rspec_model article title:string content:text
rake db:migrate
rake db:test:clone
script/generate rspec_controller articles index

Cucumber

Feature: Manage Articles
  In order to make a blog
  As an author
  I want to create and manage articles
  
  Scenario: Articles List
    Given I have articles titled Pizza, Breadsticks
    When I go to the list of articles
    Then I should see "Pizza"
    And I should see "Breadsticks"
  
  Scenario: Create Valid Article
    Given I have no articles
    And I am on the list of articles
    When I follow "New Article"
    And I fill in "Title" with "Spuds"
    And I fill in "Content" with "Delicious potato wedges!"
    And I press "Create"
    Then I should see "New article created."
    And I should see "Spuds"
    And I should see "Delicious potato wedges!"
    And I should have 1 article

config/environments/test.rb

config.gem "rspec":lib => false:version => ">=1.2.2"
config.gem "rspec-rails":lib => false:version => ">=1.2.2"
config.gem "webrat":lib => false:version => ">=0.4.3"
config.gem "cucumber":lib => false:version => ">=0.2.2"

features/step_definitions/article_steps.rb

Given /^I have articles titled (.+)$/ do |titles|
  titles.split(', ').each do |title|
    Article.create!(:title => title)
  end
end

Given /^I have no articles$/ do
  Article.delete_all
end

Then /^I should have ([0-9]+) articles?$/ do |count|
  Article.count.should == count.to_i
end

articles_controller.rb

def index
  @articles = Article.all
end

def new
  @article = Article.new
end

def create
  @article = Article.create!(params[:article])
  flash[:notice] = "New article created."
  redirect_to articles_path
end

features/support/paths.rb

def path_to(page_name)
  case page_name
  
  when /the homepage/
    root_path
  when /the list of articles/
    articles_path
  
  # Add more page name => path mappings here
  
  else
    raise "Can't find mapping from \"#{page_name}\" to a path."
  end
end

index.html.erb

<%= flash[:notice] %>
<% for article in @articles %>
  <p><%=h article.title %></p>
  <p><%=h article.content %></p>
<% end %>
<p><%= link_to "New Article", new_article_path %></p>

http://www.sevenmike.com/2011/06/20/rails3%E4%BD%BF%E7%94%A8cucumber%E5%92%8Crspec%E8%BF%9B%E8%A1%8C%E6%B5%8B%E8%AF%95/

摘自: 

posted @ 2011-10-22 21:35  fandyst  阅读(460)  评论(0编辑  收藏  举报