INSTALL=======$ gem install rspecRSPEC-RAILS=========== RAILS-3 ======= CONFIGURE THE GEMFILE ====================== group :development, :test do gem "rspec-rails", "~> 2.0" end INSTALL THE BUNDLE =============================== $ bundle install BOOTSTRAP THE APP ================= $ ./script/rails generate rspec:install create .rspec create spec create spec/spec_helper.rb create autotest create autotest/discover.rb RAILS-2 ======= INSTALL ======= $ gem install rspec-rails -v 1.3.3 BOOTSTRAP THE APP ================= $ ./script/generate rspec create spec create spec/spec_helper.rb create spec/spec.opts create previous_failures.txt create script/spec_server create script/spec
How to Use ============ command line ============ rspec -- color -- format Doc spec/widget_spec.rb rails 3 (rspec 2) ================. /rails/generate model userrake-T spec # lists all rspec rake tasksrake spec # Run all specsrake spec/models/mymodel_spec.rb # Run a single spec filerake spec/models/mymodel_spec.rb: 27 # Run a single example or group on line 27 rails 2 (rspec 1) ================. /script/generat E rspec_model userrake-T spec # lists all rspec rake tasksrake spec # Run all specsrake spec = spec/models/mymodel_spec.rb spec_opts = "-e \" shocould dosomething \ "" # Run single specmodule userspechelper def valid_user_attributes {: email => "joe@bloggs.com",: username => "joebloggs",: Password => "abcdefg"} enddescribe "a user (in general)" Do include userspechelper before (: each) do @ user = u Ser. new end it "shocould be invalid without a username" do pending "some other thing we depend on" @ user. attributes = valid_user_attributes.t T (: username) @ user. should_not be_valid @ user. shoshould have (1 ). error_on (: username) @ user. errors. on (: username ). shold = "is required" @ user. username = "someusername" @ user. shocould be_valid endendexpectations ======================== target. shocould satisfy {| arg | ...} Target. should_not satisfy {| arg | ...} target. shoshould equal <value> target. shocould not_equal <value> target. shocould be_close <value>, <tolerance> target. should_not be_close <value>, <tolerance> target. shocould be <value> target. should_not be <value> target. shocould predicate [Optional ARGs] target. shoshould be_predicate [Optional ARGs] target. should_not predicate [Optional ARGs] target. should_not be_predicate [optiona L ARGs] target. shocould be <6target. shocould = 5target. shocould be_between (1, 10) target. should_not = 'Samantha' target. shocould match <RegEx> target. should_not match <RegEx> target. shocould be_an_instance_of <class> target. should_not be_an_instance_of <class> target. shoshould be_a_kind_of <class> target. should_not be_a_kind_of <class> target. shocould respond_to <symbol> target. should_not respond_to <symbol> Lambda {a_c All }. shocould raise_errorlambda {a_call }. shocould raise_error (<exception> [, message]) Lambda {a_call }. should_not raise_errorlambda {a_call }. should_not raise_error (<exception> [, message]) Proc. shocould throw <symbol> Proc. should_not throw <symbol> target. shocould include <Object> target. should_not include <Object> target. shocould have (<number> ). thingstarget. shocould have_at_least (<number> ). thingstarget. shocould have _ At_most (<number>). thingstarget. shocould have (<number>). errors_on (: Field) CT {thing. approve! }. To change (thing,: status ). from (Status: awaiting_approval ). to (Status: Approved) specific CT {thing. destroy }. to 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 (No Tenoughcoffeeexcepion) people_stub = mock "people" lele_stub.stub! (: Each). and_yield (mock_user) lele_stub.stub! (: Bad_method ). and_raise (runtimeerror) user_stub = mock_model ("user",: Id => 23,: username => "Pat",: email => "pat@example.com") my_instance.stub! (: MSG). and_return (value) myclass. stub! (: MSG ). and_return (value) Examples (in the real world) ============================== source http://cheat.errtheblog.com/s/rspec
http://madhatted.com/2008/7/10/rspec-real-world-testingpresentation: http://kerryb.github.com/iprug-rspec-presentation/#31