10 RSpec is a Ruby library for testing.
12 - <https://rspec.info/>
17 rake -T spec # List spec tasks
21 rake spec/models/mymodel_spec.rb
22 rake spec/models/mymodel_spec.rb:27
28 describe "A User (in general)" do
29 include UserSpecHelper
31 subject { Person.new }
33 let(:admin) { Person.new(role: :admin) }
35 context "setter methods" do
36 it "should do this" do
37 pending "some other thing"
39 expect(subject.name).to eq 'x'
64 subject { CheckingAccount.new }
65 it { is_expected.to be_empty }
67 # also names: subject(:account) { ... }
74 target.should_not eq 1
76 expect(target).to eq 1
77 expect(target).not_to eq 1
85 expect(5).to equal value
86 expect(5).to be_between(1, 10)
87 expect(5).to be_within(0.05).of value
90 ### Compound expectations
93 expect(1).to (be < 2).or be > 5
96 Use `or`/`and` to string multiple matchers together. See: [Compound expectations](https://relishapp.com/rspec/rspec-expectations/docs/compound-expectations)
101 expect(x).to be value
102 expect(x).to satisfy { |arg| ... }
103 expect(x).to match /regexp/
109 expect(x).to be_zero # FixNum#zero?
110 expect(x).to be_empty # Array#empty?
111 expect(x).to have_key # Hash#has_key?
117 expect(obj).to be_an_instance_of MyClass
118 expect(obj).to be_a_kind_of MyClass
119 expect(obj).to respond_to :save!
125 expect { user.save! }.to raise_error
126 expect { user.save! }.to raise_error(ExceptionName, /msg/)
127 expect { user.save! }.to throw :symbol
130 ### Enumerables/arrays
133 expect(list).to include(<object>)
135 expect(list).to have(1).things
136 expect(list).to have_at_least(2).things
137 expect(list).to have_at_most(3).things
139 expect(list).to have(2).errors_on(:field)
145 expect { thing.approve! }.to \
146 change(thing, :status)
147 .from(Status::AWAITING_APPROVAL)
148 .to(Status::APPROVED)
150 expect { thing.destroy }.to \
151 change(Thing, :count)
158 book = double('book')
159 book = instance_double('Book', pages: 250)
165 allow(die).to receive(:roll)
166 allow(die).to receive(:roll) { 3 }
167 allow_any_instance_of(Die).to receive(:roll)
169 expect(die).to receive(:roll)
177 .with(hash_including(a: 1))
178 .with(hash_excluding(a: 1))
179 .with(array_including(:a, :b))
180 .with(array_excluding(:a, :b))
181 .with(instance_of(Fixnum))
182 .with(kind_of(Numeric))
196 https://relishapp.com/rspec/rspec-mocks/docs
201 module UserSpecHelper
202 def valid_user_attributes
203 { :email => "joe@bloggs.com",
204 :username => "joebloggs",
205 :password => "abcdefg"}
212 include UserSpecHelper