--- rspec version 11 Sat Apr 21 14:41:50 +0000 2007 +++ rspec version 31 Sun Oct 12 17:22:38 +0000 2008 @@ -1,111 +1,145 @@ INSTALL ======= + INSTALL rspec + ============= $ sudo gem install rspec + OR +$ ./script/plugin install git://github.com/dchelimsky/rspec.git -$ ./script/plugin install -svn://rubyforge.org/var/svn/rspec/tags/REL_X_Y_Z/rspec_on_rails/vendor/plugins/rs -ec_on_rails -Where X_Y_Z is the version number. + INSTALL rspec_on_rails plugin + ============================= +$ ./script/plugin install git://github.com/dchelimsky/rspec-rails.git + +BOOTSTRAP THE APP +================= $ ./script/generate rspec create spec create spec/spec_helper.rb - create spec/test2spec.erb - create test/test2spec_help.rb - create script/rails_spec - create script/rails_spec_runner + create spec/spec.opts + create previous_failures.txt + create script/spec_server + create script/spec HOW TO USE ========== + + COMMAND LINE + ============= +spec --color --format specdoc user.rspec + + RAILS + ============= ./script/generate rspec_model User module UserSpecHelper def valid_user_attributes { :email => "joe@bloggs.com", :username => "joebloggs", :password => "abcdefg"} end end -context "A User (in general)" do +describe "A User (in general)" do include UserSpecHelper - - setup do + + before(:each) do @user = User.new end - - specify "should be invalid without a username" do + + it "should be invalid without a username" do @user.attributes = valid_user_attributes.except(:username) - @user.should_not_be_valid + @user.should_not be_valid @user.errors.on(:username).should_equal "is required" + @user.should have(1).error_on(:username) @user.username = "someusername" - @user.should_be_valid + @user.should be_valid end end SHOULDA COULDA WOULDA ===================== -target.should_satisfy {|arg| ...} -target.should_not_satisfy {|arg| ...} +target.should satisfy {|arg| ...} +target.should_not satisfy {|arg| ...} -target.should_equal <value> -target.should_not_equal <value> +target.should equal <value> +target.should not_equal <value> -target.should_be_close <value>, <tolerance> -target.should_not_be_close <value>, <tolerance> +target.should be_close <value>, <tolerance> +target.should_not be_close <value>, <tolerance> -target.should_be <value> -target.should_not_be <value> +target.should be <value> +target.should_not be <value> -target.should_predicate [optional args] -target.should_be_predicate [optional args] -target.should_not_predicate [optional args] -target.should_not_be_predicate [optional args] +target.should predicate [optional args] +target.should be_predicate [optional args] +target.should_not predicate [optional args] +target.should_not be_predicate [optional args] -target.should_be < 6 +target.should be < 6 target.should == 5 target.should_not == 'Samantha' -target.should_match <regex> -target.should_not_match <regex> +target.should match <regex> +target.should_not match <regex> -target.should_be_an_instance_of <class> -target.should_not_be_an_instance_of <class> +target.should be_an_instance_of <class> +target.should_not be_an_instance_of <class> -target.should_be_a_kind_of <class> -target.should_not_be_a_kind_of <class> +target.should be_a_kind_of <class> +target.should_not be_a_kind_of <class> -target.should_respond_to <symbol> -target.should_not_respond_to <symbol> +target.should respond_to <symbol> +target.should_not respond_to <symbol> -proc.should_raise <exception> -proc.should_not_raise <exception> +*OLD:* +proc.should raise <exception> +proc.should_not raise <exception> +*NEW:* +lambda {a_call}.should raise_error +lambda {a_call}.should raise_error(<exception>) +lambda {a_call}.should_not raise_error +lambda {a_call}.should_not raise_error(<exception>) -proc.should_throw <symbol> -proc.should_not_throw <symbol> +proc.should throw <symbol> +proc.should_not throw <symbol> -target.should_include <object> -target.should_not_include <object> +target.should include <object> +target.should_not include <object> -target.should_have(<number>).things -target.should_have_at_least(<number>).things -target.should_have_at_most(<number>).things +target.should have(<number>).things +target.should have_at_least(<number>).things +target.should have_at_most(<number>).things +target.should have(<number>).errors_on(:field) +proc { thing.approve! }.should change(thing, :status). + from(Status::AWAITING_APPROVAL). + to(Status::APPROVED) + +proc { thing.destroy }.should change(Thing, :count).by(-1) + Mocks and Stubs =============== user_mock = mock "User" user_mock.should_receive(:authenticate).with("password").and_return(true) +user_mock.should_receive(:coffee).exactly(3).times.and_return(:americano) +user_mock.should_receive(:coffee).exactly(5).times.and_raise(NotEnoughCoffeeExcep +ion) -user_stub = stub("User", :id => 23, :username => "pat", :email => -"pat@example.com") - -people_stub = stub "people" +people_stub = mock "people" people_stub.stub!(:each).and_yield(mock_user) people_stub.stub!(:bad_method).and_raise(RuntimeError) + +user_stub = mock_model("User", :id => 23, :username => "pat", :email => +"pat@example.com") + +Examples (in the real world) +============================ +http://madhatted.com/2008/7/10/rspec-real-world-testing
cheat sheets.
$ cheat rspec( add new | see all )
- updated sheets

- mysql_select
- cheat
- cryptsetup
- burm
- perl
- port
- yuilang
- queriac
- mechanize
- sness
- nonsense
- haml
- git
- assert_valid
- marsmission
Advertisement