Skip to content

Instantly share code, notes, and snippets.

@kidach1
Created November 11, 2013 11:51
Show Gist options
  • Save kidach1/7412090 to your computer and use it in GitHub Desktop.
Save kidach1/7412090 to your computer and use it in GitHub Desktop.
[Rails] RSpecによるBDD(振舞駆動開発)の基本 [SporkとGuardも] ref: http://qiita.com/kidachi_/items/cb8910eb74e924456df9
--colour
--drb
$ rails new rspec_sample --skip-test-unit
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
$ bundle exec rspec spec/requests/static_pages_spec.rb
F
Failures:
1) StaticPages Home page should have the content 'Micro'
Failure/Error: expect(page).to have_content('Micro')
expected #has_content?("Micro") to return true, got false
# ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.83735 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # StaticPages Home page should have the content 'Micro'
Randomized with seed 4199
$ bundle exec rspec spec/requests/static_pages_spec.rb
.
Finished in 0.07615 seconds
1 example, 0 failures
Randomized with seed 11765
~
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
end
~
$ bundle install
#Guardを初期化し、RSpecと一緒に動作するようにする。
$ bundle exec guard init rspec
require 'active_support/inflector'
#失敗したテストが後にパスしたとき、他の余分なテストが実行されないようにする
guard 'rspec', all_after_pass: false do
~
# Custom Rails Tutorial specs
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
["spec/routing/#{m[1]}_routing_spec.rb",
"spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
"spec/acceptance/#{m[1]}_spec.rb",
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
"spec/requests/#{m[1].singularize}_pages_spec.rb")]
end
watch(%r{^app/views/(.+)/}) do |m|
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
"spec/requests/#{m[1].singularize}_pages_spec.rb")
end
watch(%r{^app/controllers/sessions_controller\.rb$}) do |m|
"spec/requests/authentication_pages_spec.rb"
end
~
end
$ bundle exec guard
~
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
~
$ bundle install
$ bundle exec spork --bootstrap
$ rails g controller <ClassName> <ActionName(任意個数)>
$ rails g controller <ClassName> <ActionName(任意個数)>
$ bundle exec spork
#--drbオプションを付ける
$ time bundle exec rspec spec/requests/static_pages_spec.rb --drb
......
6 examples, 0 failures
real 0m2.649s
user 0m1.259s
sys 0m0.258s```
$ bundle exec guard init spork
guard 'rspec', all_after_pass: false do
guard 'rspec', after_all_pass: false, cli: '--drb' do
$ bundle exec guard
18:43:40 - INFO - Starting Spork for RSpec, Test::Unit
Couldn't find a supported test framework that begins with 'testunit'
Supported test frameworks:
( ) Cucumber
(*) RSpec
Legend: ( ) - not detected in project (*) - detected
Using RSpec, Rails
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
18:44:10 - ERROR - Could not start Spork server for RSpec, Test::Unit after 30 seconds. I will continue waiting for a further 60 seconds.
18:45:10 - ERROR - Could not start Spork server for RSpec, Test::Unit. Make sure you can use it manually first.
$ kill -9 `pgrep -f 'spork'`
$ kill -9 `pgrep -f 'guard'`
$ bundle exec guard
18:43:40 - INFO - Starting Spork for RSpec, Test::Unit
Couldn't find a supported test framework that begins with 'testunit'
Supported test frameworks:
( ) Cucumber
(*) RSpec
Legend: ( ) - not detected in project (*) - detected
Using RSpec, Rails
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
18:44:10 - ERROR - Could not start Spork server for RSpec, Test::Unit after 30 seconds. I will continue waiting for a further 60 seconds.
18:45:10 - ERROR - Could not start Spork server for RSpec, Test::Unit. Make sure you can use it manually first.
-guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
+guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' }, :test_unit => false do
-guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
+guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' }, :test_unit => false do
$ rails g controller StaticPages home help --no-test-framework
$ rails g controller FooBars baz quux
//controller名とaction名
$ rails destroy controller FooBars baz quux
$ rails g model Foo bar:string baz:integer
//model名のみでok
$ rails destroy model Foo
$ rake db:migrate
//1つ前の状態に戻す
$ rake db:rollback
//最初の状態に戻す
$ rake db:migrate VERSION=0
$ rails g integration_test static_pages
~
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.9'
end
~
guard 'rspec', all_after_pass: false do
guard 'rspec', after_all_pass: false, cli: '--drb' do
<h1>Micro</h1>
<p>
This is the home page for the
<a href="http://railstutorial.jp/">Ruby on Rails Tutorial</a>
sample application.
</p>
SampleApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
~
end
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
# Include the Capybara DSL so that specs in spec/requests still work.
config.include Capybara::DSL
# Disable the old-style object.should syntax.
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "should have the content 'Micro'" do
visit '/static_pages/home'
expect(page).to have_content('Micro')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment