Created
November 19, 2013 05:06
-
-
Save FrancoB411/7540611 to your computer and use it in GitHub Desktop.
Rspec Cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Rspec Syntax Cheat Sheet | |
| ## Stub: | |
| Replacing a method with code that returns a specified result. | |
| weapon.stub(:slice) | |
| Stubbing to avoid hitting database | |
| Before: context block hits DB twice | |
| describe ZombieMailer do | |
| context '#tweet' do | |
| let(:zombie) { Zombie.create(email: 'anything@example.org') } | |
| let(:tweet) { Tweet.create(message: 'Arrrrgggghhhh', zombie: zombie) } | |
| subject { ZombieMailer.tweet(zombie, tweet) } | |
| its(:from) { should include('admin@codeschool.com') } | |
| its(:to) { should include(zombie.email) } | |
| its(:subject) { should == tweet.message } | |
| end | |
| end | |
| After: context block does not hit DB | |
| context '#tweet' do | |
| let(:zombie) { stub(email: 'anything@example.org') } | |
| let(:tweet) { stub(message: 'Arrrrgggghhhh', zombie: zombie) } | |
| Named stub by passing in a first parameter. | |
| context '#tweet' do | |
| let(:zombie) { :zombie, stub(email: 'anything@example.org') } | |
| let(:tweet) { :tweet, stub(message: 'Arrrrgggghhhh', zombie: zombie) } | |
| ## Mock: | |
| Stub with and expectation that the method gets called. | |
| This test goes ABOVE the executed code. | |
| It will not fire as the last line. | |
| subject.weapon.should_receive(:slice) | |
| In an it block: | |
| it 'calls "email_tweeter" on the tweet' do | |
| tweet.should_receive(:email_tweeter) | |
| tweet.save | |
| end | |
| ## Double | |
| Doubles are stubbed OBJECTS. Here loc is the double, and we stub out `geolocator` and return the stub object instead. | |
| loc = stub(lat: 2, long: 3) | |
| Zoogle.stub(:geolocator).returns(loc) | |
| Stubbing the email method on the zombie oject: | |
| let(:zombie) { stub(email: "email@address")} | |
| ZombieMailer mocked to expect tweet and return a mail object. | |
| ZombieMailer.should_receive(:tweet).and_return(mail) | |
| Stubbed objects, like mail, are passed in as objects, not symbols. | |
| describe Tweet do | |
| context 'after create' do | |
| let(:zombie) { Zombie.create(email: 'anything@example.org') } | |
| let(:tweet) { zombie.tweets.new(message: 'Arrrrgggghhhh') } | |
| let(:mail) { stub(deliver: true) } | |
| it 'calls "tweet" on the ZombieMailer' do | |
| ZombieMailer.should_receive(:tweet).and_return(mail) | |
| tweet.save | |
| end | |
| end | |
| end | |
| ## Verifying Arguments | |
| Specifies arguments passed into a mock. | |
| it 'calls "tweet" on the ZombieMailer' do | |
| ZombieMailer.should_receive(:tweet).with(zombie, tweet).and_return(mail) | |
| tweet.save | |
| end | |
| Verifying deliver | |
| it 'calls "deliver" on the mail object' do | |
| ZombieMailer.stub(tweet: mail) | |
| mail.should_receive(:deliver) | |
| tweet.save | |
| end | |
| More argument matchers: | |
| .with(no_args()) | |
| .with(any_args()) | |
| .with("B", anything()) | |
| .with(3, kind_of(Numeric)) | |
| .with(/zombie ash/) | |
| ## Change syntax | |
| expect{ zombie.eat_brains}.to change{zombie.iq}.by 1 | |
| ## Have matchers | |
| zombie.should have(1).tweets | |
| zombie.should have_at_least(1).tweets | |
| zombie.should have_at_most(1).tweets | |
| ## Error Syntax | |
| expect { zombie.make_decision! }.to raise_error(Zombie::NotSmartEnoughError) | |
| expect { zombie.make_decision! }.not_to raise_error(Zombie::NotSmartEnoughError) | |
| expect { zombie.make_decision! }.to_not raise_error(Zombie::NotSmartEnoughError) | |
| ## Implicit It Syntax, not 'it..do' block. | |
| describe Zombie do | |
| it { should_not be_genius } | |
| end | |
| describe Zombie do | |
| its(:iq) { should == 0 } | |
| end | |
| ## Subject Syntax | |
| context "with high iq" do | |
| subject(:zombie) { Zombie.new(iq: 3) } | |
| it { should be_genius } | |
| its(:brains_eaten_count) {should == 1 } | |
| end | |
| ## Let syntax | |
| describe Zombie do | |
| let(:tweet) { Tweet.new } | |
| let(:zombie) { Zombie.new(tweets: [tweet]) } | |
| subject{ zombie } | |
| its(:tweets) { should include(tweet) } | |
| its(:latest_tweet) { should == tweet } | |
| end | |
| Let with bang! runs before the expectation. | |
| Chained methods like "genius.count" can be strings. | |
| context "with high iq" do | |
| let!(:zombie) { Zombie.create(iq: 3, name: 'Anna') } | |
| subject { Zombie } | |
| its(:genius) { should include(zombie) } | |
| its("genius.count") { should == 1 } | |
| end | |
| ## Before(:each) syntax | |
| describe Zombie do | |
| let(:zombie) { Zombie.new } | |
| subject { zombie } | |
| before { zombie.eat_brains } | |
| it{ should_not be_dummy } | |
| it { should be_genius } | |
| end | |
| Before in a cotext block | |
| context 'with a smart zombie' do | |
| before { zombie.iq = 3 } | |
| it { should_not be_dummy } | |
| end | |
| ## Shared Example Syntax | |
| shared_examples_for 'the brainless' do | |
| it { should be_dummy } | |
| it { should_not be_genius } | |
| end | |
| describe Zombie do | |
| let(:zombie) { Zombie.new } | |
| subject { zombie } | |
| it_behaves_like "the brainless" | |
| end | |
| ## Focus Syntax | |
| Allows you to tag a spec to run it solo. | |
| context 'with a smart zombie', focus: true do | |
| before { zombie.iq = 3 } | |
| it { should_not be_dummy } | |
| end | |
| Call it with: | |
| rspec --tag focus spec/path/to/spec.rb | |
| ## Filter Syntax | |
| Filtered test with dumb | |
| context 'with a dummy zombie', dumb: true do | |
| before { zombie.iq = 0 } | |
| it { should be_dummy } | |
| end | |
| Runs ONLY tagged with dumb | |
| rspec --tag dumb spec/models/zombie_spec.rb | |
| Run NOT tagged with dumb | |
| rspec --tag ~dumb spec/models/zombie_spec.rb | |
| ## Message Counts | |
| it 'calls "tweet" on the ZombieMailer as many times as there are zombies' do | |
| Zombie.stub(all: [stub, stub, stub]) | |
| ZombieMailer.should_receive(:tweet).exactly(3).times.and_return(mail) | |
| tweet.save | |
| end | |
| More count matchers: | |
| .once | |
| .twice | |
| .exactly(n).times | |
| .at_least(2).times | |
| .at_most(n).times | |
| .any_number_of_times |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment