9 rake -T spec # List spec tasks
13 rake spec/models/mymodel_spec.rb
14 rake spec/models/mymodel_spec.rb:27
20 describe "A User (in general)" do
21 include UserSpecHelper
23 subject { Person.new }
25 let(:admin) { Person.new(role: :admin) }
27 context "setter methods" do
28 it "should do this" do
29 pending "some other thing"
31 expect(subject.name).to eq 'x'
56 subject { CheckingAccount.new }
57 it { is_expected.to be_empty }
59 # also names: subject(:account) { ... }
66 target.should_not eq 1
68 expect(target).to eq 1
69 expect(target).not_to eq 1
77 expect(5).to equal value
78 expect(5).to be_between(1, 10)
79 expect(5).to be_within(0.05).of value
82 ### Compound expectations
85 expect(1).to (be < 2).or be > 5
88 Use `or`/`and` to string multiple matchers together. See: [Compound expectations](https://relishapp.com/rspec/rspec-expectations/docs/compound-expectations)
94 expect(x).to satisfy { |arg| ... }
95 expect(x).to match /regexp/
101 expect(x).to be_zero # FixNum#zero?
102 expect(x).to be_empty # Array#empty?
103 expect(x).to have_key # Hash#has_key?
109 expect(obj).to be_an_instance_of MyClass
110 expect(obj).to be_a_kind_of MyClass
111 expect(obj).to respond_to :save!
117 expect { user.save! }.to raise_error
118 expect { user.save! }.to raise_error(ExceptionName, /msg/)
119 expect { user.save! }.to throw :symbol
122 ### Enumerables/arrays
125 expect(list).to include(<object>)
127 expect(list).to have(1).things
128 expect(list).to have_at_least(2).things
129 expect(list).to have_at_most(3).things
131 expect(list).to have(2).errors_on(:field)
137 expect { thing.approve! }.to \
138 change(thing, :status)
139 .from(Status::AWAITING_APPROVAL)
140 .to(Status::APPROVED)
142 expect { thing.destroy }.to \
143 change(Thing, :count)
150 book = double('book')
151 book = instance_double('Book', pages: 250)
157 allow(die).to receive(:roll)
158 allow(die).to receive(:roll) { 3 }
159 allow_any_instance_of(Die).to receive(:roll)
161 expect(die).to receive(:roll)
169 .with(hash_including(a: 1))
170 .with(hash_excluding(a: 1))
171 .with(array_including(:a, :b))
172 .with(array_excluding(:a, :b))
173 .with(instance_of(Fixnum))
174 .with(kind_of(Numeric))
188 https://relishapp.com/rspec/rspec-mocks/docs
193 module UserSpecHelper
194 def valid_user_attributes
195 { :email => "joe@bloggs.com",
196 :username => "joebloggs",
197 :password => "abcdefg"}
204 include UserSpecHelper