Skip to content

Instantly share code, notes, and snippets.

View dchelimsky's full-sized avatar

David Chelimsky dchelimsky

  • Retired
  • Chicago, IL, USA
View GitHub Profile
require 'rubygems'
require 'action_controller/base'
class SomeController < ActionController::Base
def index
render :text => "this text"
end
end
describe SomeController do
# Here are a couple of alternative suggestions for PSpec (see http://gist.github.com/305174).
# I'm not in love with any of these ideas, but hopefully they'll provide some food for thought.
#
# I like using strings for test names, so I like the 'with xxx' syntax, but I think that
# the 'with describe' doesn't makke sense - how about 'with subject':
with subject('calculator'):
with expectation('adds positive integers'):
expect(Calculator.add(1, 1)) == 2
with expectation('adds negative integers'):
describe Person do
context "walking on a rainy day" do
context "with an umbrella" do
it "stays dry" do
day = Day.new(:weather => Rain.new)
person = Person.new(:umbrella => Umbrella.new)
person.walk_on(day)
person.should be_dry
end
end
Spec::Matchers.define :have_a_machinist_blueprint do
match do |model|
model.class.respond_to?(:make)
end
description do
"works if there's a valid blueprint"
end
failure_message_for_should do |model|
def transactions(on_or_off=:on)
if on_or_off == :off
prepend_before { @transactions = :off }
end
end
Spec::Runner.configure do |c|
c.before(:each) do
@transactions ||= :on
Rspec.configure do |c|
c.before do
unless running_example.metadata[:transactions] == :off
puts "RUNNING TRANSACTIONS"
end
end
end
describe "foo", :transactions => :on do
it "does something" do
class Photo
belongs_to :photo_album
def set_cover(cover)
if cover
photo_album.cover = self
elsif photo_album.cover.nil?
# set album cover to first photo album if no cover has been set.
photo_album.cover = photo_album.photos.first
$ autotest
loading autotest/rails_rspec2
style: RailsRspec2
--------------------------------------------------------------------------------
/Users/dchelimsky/.rvm/rubies/ruby-1.8.7-p249/bin/ruby -rrubygems /Users/dchelimsky/.rvm/gems/ruby-1.8.7-p249/gems/rspec-core-2.0.0.beta.6/bin/rspec /Users/dchelimsky/tmp/example/spec/models/widget_spec.rb /Users/dchelimsky/tmp/example/spec/views/widgets/show.html.erb_spec.rb /Users/dchelimsky/tmp/example/spec/views/widgets/edit.html.erb_spec.rb /Users/dchelimsky/tmp/example/spec/views/widgets/new.html.erb_spec.rb /Users/dchelimsky/tmp/example/spec/controllers/widgets_controller_spec.rb /Users/dchelimsky/tmp/example/spec/views/widgets/index.html.erb_spec.rb
*...................
$ rake spec
(in /Users/dchelimsky/projects/ruby/rspec2/repos/rspec-rails/tmp/example_app)
DEPRECATION WARNING: Reading protected_instance_variables directly from ActionController::Base is deprecated. Please read it from config.protected_instance_variables. (called from view_assigns at /Users/dchelimsky/projects/ruby/rspec2/repos/rspec-rails/tmp/rails/actionpack/lib/abstract_controller/rendering.rb:129)
FDEPRECATION WARNING: Reading protected_instance_variables directly from ActionController::Base is deprecated. Please read it from config.protected_instance_variables. (called from view_assigns at /Users/dchelimsky/projects/ruby/rspec2/repos/rspec-rails/tmp/rails/actionpack/lib/abstract_controller/rendering.rb:129)
FDEPRECATION WARNING: Reading protected_instance_variables directly from ActionController::Base is deprecated. Please read it from config.protected_instance_variables. (called from view_assigns at /Users/dchelimsky/projects/ruby/rspec2/repos/rspec-rails/tmp/rails/actionpack/lib/abstract_controller/re
$ diff app-head app-beta3 -r
diff -r app-head/app/views/layouts/application.html.erb app-beta3/app/views/layouts/application.html.erb
4c4
< <title>AppHead</title>
---
> <title>AppBeta3</title>
diff -r app-head/config/application.rb app-beta3/config/application.rb
9c9
< module AppHead
---