Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RSpec: specs for better specs

RSpec: specs for better specs

Personal learning experiences related with RSpec usage, and general tooling to specs writing.

Brenno Costa

April 07, 2013
Tweet

Other Decks in Technology

Transcript

  1. Quem sou eu? Brenno Costa @brennovich Developer at Codeminer42, that

    plays guitar with your teeth. Brasil, RN, Natal · github.com/brennovich
  2. Atenção! - Não são abordagens generalistas - Bom senso e

    contexto contam (muito) - Opiniões e Experiências pessoais
  3. 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
  4. 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
  5. 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
  6. Dicas: Factory Girl - Teste de sanidade ‘has a valid

    factory’ - Traits - FactoryGirl::Syntax::Methods
  7. Exemplo 2 - Uso do subject - Evitar tocar na

    database - Dica: stubbed_sign_in
  8. 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
  9. 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
  10. 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
  11. ... 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 }
  12. 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
  13. 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
  14. 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/)
  15. Quem sou eu? Brenno Costa @brennovich Developer at Codeminer42, that

    plays guitar with your teeth. Brasil, RN, Natal · github.com/brennovich