$ gem install rspec
RSPEC-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 User
rake -T spec # lists all rspec rake tasks
rake spec # run all specs
rake spec/models/mymodel_spec.rb # run a single spec file
rake spec/models/mymodel_spec.rb:27 # run a single example or group on line 27
RAILS 2 (RSPEC 1)
=============
./script/generate rspec_model User
rake -T spec # lists all rspec rake tasks
rake spec # run all specs
rake spec SPEC=spec/models/mymodel_spec.rb SPEC_OPTS="-e \"should do
something\"" #run a single spec
module UserSpecHelper
def valid_user_attributes
{ :email => "joe@bloggs.com",
:username => "joebloggs",
:password => "abcdefg"}
end
end
describe "A User (in general)" do
include UserSpecHelper
before(:each) do
@user = User.new
end
it "should be invalid without a username" do
pending "some other thing we depend on"
@user.attributes = valid_user_attributes.except(:username)
@user.should_not be_valid
@user.should have(1).error_on(:username)
@user.errors.on(:username).should == "is required"
@user.username = "someusername"
@user.should be_valid
end
end
EXPECTATIONS
=====================
target.should satisfy {|arg| ...}
target.should_not satisfy {|arg| ...}
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 <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 be < 6
target.should == 5
target.should be_between(1, 10)
target.should_not == 'Samantha'
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_a_kind_of <class>
target.should_not be_a_kind_of <class>
target.should respond_to <symbol>
target.should_not respond_to <symbol>
lambda {a_call}.should raise_error
lambda {a_call}.should raise_error(<exception> [, message])
lambda {a_call}.should_not raise_error
lambda {a_call}.should_not raise_error(<exception> [, message])
lambda {a_call}.should change(instance, method).from(number).to(number)
proc.should throw <symbol>
proc.should_not throw <symbol>
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>).errors_on(:field)
expect { thing.approve! }.to change(thing, :status).
from(Status::AWAITING_APPROVAL).
to(Status::APPROVED)
expect { 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(NotEnoughCoffeeExcep
ion)
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")
my_instance.stub!(:msg).and_return(value)
MyClass.stub!(:msg).and_return(value)
Examples (in the real world)
skip to main |
skip to sidebar
Pages
Monday, 12 November 2012
another rspec cheat list
http://cheat.errtheblog.com/s/rspec/
Labels
- add_column (2)
- alias (1)
- Android (2)
- api (2)
- arguments (1)
- autocomplete (1)
- bang (1)
- border (1)
- browser (1)
- check (1)
- css (1)
- csv (1)
- darnket (1)
- data (2)
- database (1)
- db:migrate (1)
- debugger (1)
- default (1)
- default user (1)
- development (2)
- down (1)
- effects (1)
- Emacs (4)
- Emacs24 (1)
- eventmachine (2)
- expectations (1)
- factory girl (2)
- file (1)
- first (1)
- focus (1)
- function (1)
- gem (1)
- git (2)
- highlights (1)
- html (1)
- ie7 (1)
- index (1)
- internet (1)
- Iphone (1)
- javascript (3)
- jquery (3)
- jquery-ui (1)
- JQUERY. toggleClass (1)
- key biding (1)
- mac (2)
- mac osx (3)
- method (1)
- migrations (2)
- mock (2)
- mocks (2)
- mongodb (1)
- MySQL (3)
- namespace (1)
- operators (1)
- optimizing (1)
- password (1)
- popup (1)
- postgresql (2)
- RAIL_ENV (1)
- rails (16)
- regex (1)
- regular expressions (1)
- remove_column (1)
- repair (1)
- repository (1)
- resize (1)
- rspec (7)
- rspecs (6)
- ruby (16)
- ruby on rails (12)
- sdk (1)
- select (2)
- server (1)
- shortcut (1)
- specs (3)
- stash (1)
- stub (4)
- stub_chain (1)
- terminal (2)
- test (1)
- traits (1)
- trusted source (1)
- tutorial (1)
- Ubuntu (6)
- validation (1)
- value (1)
- viewport (1)
- windows (1)
- windows7 (1)
Blog Archive
-
▼
2012
(41)
-
▼
November
(11)
- android sdk on mac osx
- ruby regex expressions
- a theoretical problem with rails models
- another rspec cheat list
- Validation is very useful in rails
- Factory Girl is awesome
- rails bang methods
- javascript default value for argument
- how to comunicate if the internet is down for good
- rails generate add column to table
- some javascript interesting stuff
-
▼
November
(11)
Powered by Blogger.
0 comments:
Post a Comment