Rspec tips - SumitBisht/checklist_and_guides GitHub Wiki

Testing With Rspec

Syntax

describe Account
	it "has more than 1 repo" do
		account = Account.new
		account.repos.length.should > 1
												 ------ 
												modifier
																-----
															 matcher
	end
end

Running Rspec

Running with tags

rspec --tag focus

Running all except with tag

rspec --tag ~focus

Initialize rspec in a ruby project

rspec --init

Extra Matchers

Have

if you have the following expectation

account.repos.count.should == repos_number

it can be rewritten like

account.should have(repos_number).repos

Expect-Change

expect { account.add_repo(repo) }.to change { account.repos.count }.by(1)

Raise Error

expect { current_user.account.add_repo(other_user_repo) }.to raise_error(OperationNotAllowed)

Other matchers

to
not_to
to_not
respond_to(<method_name>)
be_within(<range>).of(<expected>)
exist
satisfy { <block> }
be_kind_of(<class>)
be_a_instance_of(<class>)
have(n)
have_at_least(n)
have_at_most(n)

Rspec Concepts

Implicit Receiver

in a expectation, if you call a matcher without any params it will be called against the class subject, this is called implicit receiver

describe Account
	it "responds to repos" do
		account = Account.new
		account.should respond_to(:repos)
	end
end

it's the same as

describe Account
	it "responds to repos" do
		should respond_to(:repos)
	end
end

Short Syntax for IT

it "responds to name" do
	should respond_to(:name)
end

can be rewritten as

it { should respond_to(:name) }

ITS

its is for represent an expectation on a subject attribute or method

describe Account do
	it { subject.organization.should be_nil }
end

it's the same as

describe Account do
	its(:organization) { should be_nil }
end

and, its even works with chained methods as string

describe Account do
	its('owner.name') { should be_nil }
end

Callbacks

Before

it runs before each example

before(:each) do ... end

it's equivalent to

before { ... }

Stubs

For replacing a method with code that returns a specified result.

Syntax

object.stub(:fake_method_name_on_object, {method: result})

Mocks

A stub with an expectations that the method gets called.

object.should_receive(:method).with(params).and_return(return_value)

Expect a number of calls

this can be achieved with

target.should_receive(:function).once
                                .twice
                                .exactly(3).times
                                .at_least(2).times
                                .at_most(3).times
                                .any_number_of_times

Parameters

you can modify the arguments of a mock like this:

target.should_receive(:function).with(no_args())
                                .with(any_args())
                                .with("B", anything())
                                .with(3, kind_of(Numeric))
                                .with(/zombie ash/)

Shared Example

pending...

Custom Matchers

pending...

⚠️ **GitHub.com Fallback** ⚠️