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
86 expect(x).to satisfy { |arg| ... }
87 expect(x).to match /regexp/
93 expect(x).to be_zero # FixNum#zero?
94 expect(x).to be_empty # Array#empty?
95 expect(x).to have_key # Hash#has_key?
101 expect(obj).to be_an_instance_of MyClass
102 expect(obj).to be_a_kind_of MyClass
103 expect(obj).to respond_to :save!
109 expect { user.save! }.to raise_error
110 expect { user.save! }.to raise_error(ExceptionName, /msg/)
111 expect { user.save! }.to throw :symbol
114 ### Enumerables/arrays
117 expect(list).to include(<object>)
119 expect(list).to have(1).things
120 expect(list).to have_at_least(2).things
121 expect(list).to have_at_most(3).things
123 expect(list).to have(2).errors_on(:field)
129 expect { thing.approve! }.to \
130 change(thing, :status)
131 .from(Status::AWAITING_APPROVAL)
132 .to(Status::APPROVED)
134 expect { thing.destroy }.to \
135 change(Thing, :count)
142 book = double('book')
143 book = instance_double('Book', pages: 250)
149 allow(die).to receive(:roll)
150 allow(die).to receive(:roll) { 3 }
151 allow_any_instance_of(Die).to receive(:roll)
153 expect(die).to receive(:roll)
161 .with(hash_including(a: 1))
162 .with(hash_excluding(a: 1))
163 .with(array_including(:a, :b))
164 .with(array_excluding(:a, :b))
165 .with(instance_of(Fixnum))
166 .with(kind_of(Numeric))
180 https://relishapp.com/rspec/rspec-mocks/docs
185 module UserSpecHelper
186 def valid_user_attributes
187 { :email => "joe@bloggs.com",
188 :username => "joebloggs",
189 :password => "abcdefg"}
196 include UserSpecHelper