rspec

Post on 15-Jan-2015

1.151 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

RSpecdocumentation

http://relishapp.com/rspec

componentsRSpec Core

RSpec Expectations

RSpec Mocks

RSpec Rails

run specsrake specrake spec:modelsrspec spec/models/order_spec.rbrspec spec/models/order_spec.rb:19

"be" matchersdescribe 0 do it { subject.zero?.should == true } # vs. it { should be_zero } # 0.zero? == trueend

have(n).items matchercollection.should have(x).itemscollection.should have_at_least(x).itemscollection.should have_at_most(x).itemsit { should have(5).line_items }

subject

implicit subjectdescribe Comment do subject { Comment.new }end

explicit subjectdescribe Order do subject { Factory.build(:order) }end

implicit receiverit { subject.should be_valid }# vs.

it { should be_valid }

itsits(:status_name) { should == :draft }its('line_items.count') { should == 5 }

letbefore do @order = Factory.create(:shipped_order)end# vs.

let(:order) do Factory.create(:shipped_order)end

context vs. describealias :context :describe

describe is for methods

context is for contexts

class & instance methodsdescribe '#title' do # instance method ...enddescribe '.title' do # class method ...end

contextcontext 'when quantity is negative' do before { subject.quantity = -1 } ...end

model validationsit { should be_invalid }its(:errors) { should be_present }it { should have(1).errors_on(:quantity) }

shoulda-matchersit 'should validate email and quantity' do subject.email = 'test@example.com' subject.quantity = -1 subject.valid? should have(:no).errors_on(:email) should have_at_least(1).error_on(:quantity)end# vs.

it { should allow_value('test@example.com').for(:email) }it { should_not allow_value(-1).for(:quantity) }

mocksfake objects and methods

don't touch database

faster specs

rails model mocksmock_model is a test double that acts like an

ActiveModel

stub_model is a instance of a realActiveModel with some methods stubbed

using mockslet(:email_template) do stub_model(EmailTemplate, :name => 'test_email', ...)endbefore do ... EmailTemplate.stub(:find_by_name). with('test_email'). and_return(email_template)end

message expectationdef should_deliver_mail(method) Mailer.should_receive(method). with(subject). and_return(double(:deliver => nil))end

questions?

top related