Share
Embed
RSpec: specs for better specs
Personal learning experiences related with RSpec usage, and general tooling to specs writing.
Brenno Costa
April 07, 2013
Transcript
25 slides
-
Quem sou eu? Brenno Costa @brennovich Developer at Codeminer42, that
plays guitar with your teeth. Brasil, RN, Natal · github.com/brennovich -
De onde eu VIM? - Engenharia de Materiais - Lunaltas
- Codeminer42 -
Atenção! - Não são abordagens generalistas - Bom senso e
contexto contam (muito) - Opiniões e Experiências pessoais -
describe User do let(:brenno) { create :brenno } it 'has
a valid factory' do brenno.should be_valid end it 'responds to admin? method' do brenno.respond_to?(:admin?).should be_true brenno.admin?.should be_true end end -
describe User do subject(:user) { build :user } it 'has
a valid factory' do expect(user).to be_valid end describe '#admin?' do context 'when user is an admin' do subject(:user) { build_stubbed :user, :admin } it 'returns true' do expect(user.admin?).to be_true end end context 'when user is a content manager' do subject(:user) { build_stubbed :user, :content_manager } it 'returns false' do expect(user.admin?).to be_false end end end end -
User has a valid fixture responds to admin? method Documentação
User has a valid fixture #admin? when user is an admin returns true when user is a content manager returns false -
Dicas: Factory Girl - Teste de sanidade ‘has a valid
factory’ - Traits - FactoryGirl::Syntax::Methods -
RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = :expect
end end Dicas: RSpec -
Exemplo 2 - Uso do subject - Evitar tocar na
database - Dica: stubbed_sign_in -
describe 'GET#show' do let(:admin) { create :brenno } let(:user) {
create :johndoe } subject { get :show, :id => user } context 'as user' do before { stubbed_sign_in user } context 'accessing others accounts' do subject { get :show, :id => admin } it 'redirects' do expect(subject).to redirect_to user_root_path end end context 'accessing your own account' do subject { get :show, :id => user } it 'success' do expect(subject).to be_success end it 'assigns @user' do subject expect(assigns :user).to eql user end end end end -
describe 'GET#show' do before { stubbed_sign_in user } context 'as
user' do let(:user) { build_stubbed :user } context 'accessing others accounts' do before do admin = build_stubbed(:admin) User.stub(:find).and_return(admin) get :show, :id => admin end it 'redirects to user dashboard' do expect(response).to redirect_to user_root_path end end context 'accessing your own account' do before do User.stub(:find).and_return(admin) get :show, :id => user end it 'responds successfully' do expect(response).to be_success end it 'assigns user to @user' do expect(assigns :user).to eql user end end end end -
def stubbed_sign_in user request.env['warden'].stub :authenticate! => user controller.stub :current_user =>
user end Dicas: stubbed_sign_in -
describe Dashboard::CoordinatorsController do before { stubbed_sign_in user if user }
describe 'GET#index' do before { get :index } it_behaves_like 'restrictable' context 'when accessed by an admin' do let(:user) { build_stubbed :administrator } context 'with one coordinator registered' do let(:coordinator) { build :coordinator } around do |example| Coordinator.should_receive(:page).and_return([coordinator]) example.run end it { expect(assigns :coordinators).to have_exactly(1).coordinator } end end end end -
... describe '#set_random_password!' do let(:user) { described_class.new } it 'sets
a new random password' do user.set_random_password! user.encrypted_password.should_not be nil end end end describe(User) { it_behaves_like PasswordSecured } -
Exemplo 4 - Dica do Uncle bob - Dica da
Sarah Mei -
describe ".eaten_most_of_cocadas_in_weeks?" do context 'of three weeks' do (1..3).each do
|number_of_eaten_cocadas| most_eaten_answer = number_of_eaten_cocadas > 1 ? true : false it "returns #{most_eaten_answer} when #{number_of_eaten_cocadas}/3 is eaten" do time_travel do eat_cocadas(number_of_eaten_cocadas) tiago.eaten_most_of_cocadas_in_weeks?(3).should eql(most_eaten_answer) end end end end end -
describe '.eaten_most_of_cocadas_in_weeks?' do context 'when there are cocadas of 3
weeks' do context 'with 1/3 is eaten' do it 'returns false' do time_travel do eat_cocadas 1 expect(tiago.eaten_most_of_cocadas_in_weeks? 3).to eql false end end end context 'with 2/3 is eaten' do it 'returns true' do time_travel do eat_cocadas 2 expect(tiago.eaten_most_of_cocadas_in_weeks? 3).to eql true end end end end end -
Links 1. [Better Specs](http://betterspecs.org) 2. [Why test factories](http://robots.thoughtbot.com/post/ 22670085288/use-factory-girls-build-stubbed-for-a-faster-test) 3.
[thoughtbot podcast](http://learn.thoughtbot.com/podcast/24) 4. [named subject] (http://blog.davidchelimsky.net/2012/05/13/ spec-smell-explicit-use-of-subject/) -
Quem sou eu? Brenno Costa @brennovich Developer at Codeminer42, that
plays guitar with your teeth. Brasil, RN, Natal · github.com/brennovich