Rspec tips - SumitBisht/checklist_and_guides GitHub Wiki
describe Account
it "has more than 1 repo" do
account = Account.new
account.repos.length.should > 1
------
modifier
-----
matcher
end
end
rspec --tag focus
rspec --tag ~focus
rspec --init
if you have the following expectation
account.repos.count.should == repos_number
it can be rewritten like
account.should have(repos_number).repos
expect { account.add_repo(repo) }.to change { account.repos.count }.by(1)
expect { current_user.account.add_repo(other_user_repo) }.to raise_error(OperationNotAllowed)
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)
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
it "responds to name" do
should respond_to(:name)
end
can be rewritten as
it { should respond_to(:name) }
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
it runs before each example
before(:each) do ... end
it's equivalent to
before { ... }
For replacing a method with code that returns a specified result.
object.stub(:fake_method_name_on_object, {method: result})
A stub with an expectations that the method gets called.
object.should_receive(:method).with(params).and_return(return_value)
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
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/)
pending...
pending...