Your SlideShare is downloading. ×
0
Tips for better tests
Oto Brglez / March 2014
otobrglez.opalab.com
#1 Describing methods?
describe 'the authenticate method for User' do
describe 'if the user is an admin' do
describe '.aut...
#2 Missing some context?
it 'has 200 status code if logged in' do
expect(response).to respond_with 200
end
it 'has 401 sta...
#3 Descriptions...
it 'has 422 status code if an unexpected params will be added' do
context 'when not valid' do
it { shou...
#4 Who?
it { expect(assigns('message')).to match /it was born in Maribor/ }
it { expect(assigns('message').creator).to mat...
#5 Be lazy...
describe '#type_id' do
before { @resource = FactoryGirl.create :device }
before { @type = Type.find @resourc...
#6 See this a lot?
user = User.create(
first_name: 'Oto',
last_name: 'Brglez',
country: 'Slovenia',
email: 'otobrglez@gmai...
#7 Can’t remember...
lambda { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
expect { model.save! }.to rai...
#8 But it works just like...
describe 'GET /devices' do
let!(:resource ) { FactoryGirl .create :device, created_from : use...
#9 Me, you, him? When?
it 'should not change timings' do
consumption.occur_at.should == valid.occur_at
end
it 'does not ch...
#10 I don’t have the time...
rspec spec/models/user_spec.rb
1. bundle exec guard # or zeus, spork, spin
2. rspec spec/mode...
#11 External is slow. :/
require 'open-uri'
file = open('http://otobrglez.opalab.com')
contents = file.read
puts contents
...
What else?
1. Use formatters
2. Test what you see
3. Test edge-cases
4. Learn how to mock/stub
5. Decuple your code
6. Thi...
Thanks
Inspired by betterspecs.org and nasty test suites.
Read my blog; and be ql.
- Oto
Upcoming SlideShare
Loading in...5
×

Oto Brglez - Tips for better tests

406

Published on

Practical tips on how to improve your RSpec specs and test suites in general. Slides ware presented on Slovenian Ruby Meetup on March 2014.

Published in: Software, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
406
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Oto Brglez - Tips for better tests"

  1. 1. Tips for better tests Oto Brglez / March 2014 otobrglez.opalab.com
  2. 2. #1 Describing methods? describe 'the authenticate method for User' do describe 'if the user is an admin' do describe '.authenticate' do describe '#admin?' do
  3. 3. #2 Missing some context? it 'has 200 status code if logged in' do expect(response).to respond_with 200 end it 'has 401 status code if not logged in' do expect(response).to respond_with 401 end context 'when logged in' do it { should respond_with 200 } end context 'when logged out' do it { should respond_with 401 } end
  4. 4. #3 Descriptions... it 'has 422 status code if an unexpected params will be added' do context 'when not valid' do it { should respond_with 422 } end
  5. 5. #4 Who? it { expect(assigns('message')).to match /it was born in Maribor/ } it { expect(assigns('message').creator).to match /Ljubljana/ } subject { assigns('message') } it { should match /it was born in Ljubljana/ }
  6. 6. #5 Be lazy... describe '#type_id' do before { @resource = FactoryGirl.create :device } before { @type = Type.find @resource.type_id } it 'sets the type_id field' do expect(@resource.type_id).to equal(@type.id) end end describe '#type_id' do let(:resource) { FactoryGirl.create :device } let(:type) { Type.find resource.type_id } it 'sets the type_id field' do expect(resource.type_id).to equal(type.id) end end
  7. 7. #6 See this a lot? user = User.create( first_name: 'Oto', last_name: 'Brglez', country: 'Slovenia', email: 'otobrglez@gmail.com' , active: true ) user = FactoryGirl.create :user
  8. 8. #7 Can’t remember... lambda { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound expect { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
  9. 9. #8 But it works just like... describe 'GET /devices' do let!(:resource ) { FactoryGirl .create :device, created_from : user.id } let(:uri) { '/devices' } context 'when shows all resources' do let!( :not_owned ) { FactoryGirl .create factory } it 'shows all owned resources' do page.driver.get uri expect(page.status_code).to be( 200) contains_owned_resource resource does_not_contain_resource not_owned end end describe '?start=:uri' do it 'shows the next page' do page.driver.get uri, start : resource.uri expect(page.status_code).to be( 200) contains_resource resources.first expect(page).to_not have_content resource.id.to_s end end end describe 'GET /devices' do let!(:resource) { FactoryGirl.create :device } let(:uri) { '/devices' } it_behaves_like 'a listable resource' it_behaves_like 'a paginable resource' it_behaves_like 'a searchable resource' it_behaves_like 'a filterable list' end
  10. 10. #9 Me, you, him? When? it 'should not change timings' do consumption.occur_at.should == valid.occur_at end it 'does not change timings' do expect(consumption.occur_at).to equal(valid.occur_at) end
  11. 11. #10 I don’t have the time... rspec spec/models/user_spec.rb 1. bundle exec guard # or zeus, spork, spin 2. rspec spec/models/user_spec.rb -l 10 3. rspec --no-drb spec/models/*
  12. 12. #11 External is slow. :/ require 'open-uri' file = open('http://otobrglez.opalab.com') contents = file.read puts contents context "blog" do let(:uri) { 'http://otobrglez.opalab.com' } before { stub_request(:get, uri).to_return(status: 200, body: fixture('home.html')) } it "gets home page" do # magic here... expect(page).to have_content 'Oto Brglez' end end
  13. 13. What else? 1. Use formatters 2. Test what you see 3. Test edge-cases 4. Learn how to mock/stub 5. Decuple your code 6. Think about CI 7. Keep your tests fast and readable 8. Learn from OS projects
  14. 14. Thanks Inspired by betterspecs.org and nasty test suites. Read my blog; and be ql. - Oto
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×