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

Straight Up Rspec: A Neat Ruby BDD Tool

PromptWorks
February 21, 2014
20

Straight Up Rspec: A Neat Ruby BDD Tool

PromptWorks

February 21, 2014
Tweet

More Decks by PromptWorks

Transcript

  1. martini_spec.rb describe "Barkeep::Martini" do before do @martini = Barkeep::Martini.new end

    it "should have an attribute named booze" do @martini.should respond_to(:booze) @martini.should respond_to(:booze=) end it "should have an attribute named garnish" do @martini.should respond_to(:garnish) @martini.should respond_to(:garnish=) end end Martini Spec Draft
  2. have_attribute.rb module AttributeMatchers ! Spec::Matchers.define :have_attribute do |name| match do

    |target| getter = name.to_sym setter = (name.to_s + "=").to_sym target.respond_to?(getter) && target.respond_to?(setter) end end ! end Custom Matcher
  3. martini_spec.rb describe "Barkeep::Martini" do include AttributeMatchers before do @martini =

    Barkeep::Martini.new end it "should have an attribute named booze" do @martini.should have_attribute(:booze) end it "should have an attribute named garnish" do @martini.should have_attribute(:garnish) end end Custom Matcher
  4. martini_spec.rb describe "Barkeep::Martini" do include AttributeMatchers subject { Barkeep::Martini.new }

    it "should have an attribute named booze" do subject.should have_attribute(:booze) end it "should have an attribute named garnish" do subject.should have_attribute(:garnish) end end subject()
  5. martini_spec.rb describe "Barkeep::Martini" do include AttributeMatchers subject { Barkeep::Martini.new }

    it "should have an attribute named booze" do should have_attribute(:booze) end it "should have an attribute named garnish" do should have_attribute(:garnish) end end Implicit subject()
  6. martini_spec.rb describe "Barkeep::Martini" do include AttributeMatchers subject { Barkeep::Martini.new }

    it { should have_attribute(:booze) } it { should have_attribute(:garnish) } end DRY Messages
  7. martini_spec.rb describe Barkeep::Martini do it { should have_attribute(:booze) } it

    { should have_attribute(:garnish) } end config.include()
  8. martini_spec.rb describe "Barkeep::Martini" do before do @martini = Barkeep::Martini.new end

    it "should have an attribute named booze" do @martini.should respond_to(:booze) @martini.should respond_to(:booze=) end it "should have an attribute named garnish" do @martini.should respond_to(:garnish) @martini.should respond_to(:garnish=) end end Original Draft
  9. martini_spec.rb describe Barkeep::Martini do it { should have_attribute(:booze) } it

    { should have_attribute(:glass) } it { should have_attribute(:garnish) } end Martini Spec
  10. support/examples/drink.rb share_examples_for "a drink" do it { should have_attribute(:booze) }

    it { should have_attribute(:glass) } end Shared Example Group
  11. support/examples/drink.rb share_examples_for "a drink" do it { should have_attribute(:booze) }

    it { should have_attribute(:glass) } ! it "should have an ingredients Array" do subject.ingredients.should be_a Array end end Whiskey & Martini #ingredients
  12. martini_spec.rb describe Barkeep::Martini do it_should_behave_like "a drink" it { should

    have_attribute(:garnish) } it { should have_attribute(:mixer) } context "with a mixer" do before do @mixer = 'vermouth' subject.mixer = @mixer end it "should include mixer in ingredients" do subject.ingredients.should include @mixer end end context()
  13. martini_spec.rb describe Barkeep::Martini do it_should_behave_like "a drink" it { should

    have_attribute(:garnish) } it { should have_attribute(:mixer) } context "with a mixer" do let(:mixer) { 'vermouth' } before { subject.mixer = mixer } it "should include mixer in ingredients" do subject.ingredients.should include mixer end end end let()
  14. martini_spec.rb describe Barkeep::Martini do it_should_behave_like "a drink" it { should

    have_attribute(:garnish) } it { should have_attribute(:mixer) } context "with a mixer" do let(:mixer) { 'vermouth' } before { subject.mixer = mixer } its(:ingredients) { should include mixer } end end its()
  15. martini_spec.rb describe Barkeep::Martini do it_should_behave_like "a drink" it { should

    have_attribute(:garnish) } it { should have_attribute(:mixer) } end ! describe Barkeep::Martini, ".new with mixer" do let(:mixer) { 'vermouth' } subject { Barkeep::Martini.new(:mixer => mixer) } its(:ingredients) { should include mixer } end its()
  16. martini_spec.rb describe Barkeep::Martini, ".new with mixer" do let(:mixer) { 'vermouth'

    } subject { Barkeep::Martini.new(:mixer => mixer) } its(:ingredients) { should include mixer } its(:ingredients) { should have(1).items } end have()
  17. martini_spec.rb describe Barkeep::Martini, ".new with mixer" do let(:mixer) { 'vermouth'

    } subject { Barkeep::Martini.new(:mixer => mixer) } its(:ingredients) { should include mixer } it { should have(1).ingredients } end have()
  18. martini_spec.rb describe Barkeep::Martini do let(:mixer) { 'vermouth' } it "should

    include mixer in ingredients" do expect { subject.mixer = mixer }. to change { subject.ingredients }. from( [] ). to [ mixer ] end end expect()
  19. terminal_spec.rb describe Barkeep::Terminal, "#printer" do let(:ones) { "1.1.1.1" } let(:epson)

    { double("Espon 5000") } subject { Barkeep::Terminal.new(:ip => ones) } ! before do Printer.stub(:new) { epson } end ! it "should have a printer" do subject.printer.should eq epson end end stub()
  20. terminal_spec.rb describe Barkeep::Terminal, "#printer" do let(:ones) { "1.1.1.1" } let(:epson)

    { double("Espon 5000") } subject { Barkeep::Terminal.new(:ip => ones) } ! it "should have a printer" do Printer.should_receive(:new). with(:ip => ones) { epson } subject.printer end end should_receive()
  21. support/configure.rb RSpec.configure do |config| config.include MyMartiniMatchers config.include ModelHelpers, :type =>

    :model end ! ! # spec/model/martini_spec.rb ! describe Martini, :type => :model do … end RSpec.configure
  22. support/configure.rb RSpec.configure do |config| config.before(:all) {} # run once config.before(:each)

    {} # run before each example config.after (:each) {} # run after each example config.after (:all) {} # run once ! # run before each example of type :model config.before(:each, :type => :model) {} end RSpec.configure
  23. support/configure.rb RSpec.configure do |c| c.filter_run :focus => true c.run_all_when_everything_filtered =

    true end ! # in any spec file describe "something" do it "does something", :focus => true do # .... end end Inclusion
  24. support/configure.rb RSpec.configure do |c| c.exclusion_filter = { :ruby => lambda

    {|version| !(RUBY_VERSION.to_s =~ /^#{version.to_s}/) }} end ! # in any spec file describe "something" do it "does something", :ruby => 1.8 do # .... end it "does something", :ruby => 1.9 do # .... Exclusion
  25. describe User do it { should belong_to(:account) } it {

    should have_many(:posts) } it { should validate_presence_of(:email) } it { should allow_value("[email protected]").for(:email) } it { should_not allow_value("test").for(:email) } end
  26. spec/user_spec.rb describe User, ".admin_names" do let(:admin) { User.create!(:admin => true,

    :first => "Clark", :last => "Kent")} let(:avg_joe) { User.create!(:admin => false, :first => "Joe", :last => "Shmo")} subject { admin_names } ! it { should include "Clark Kent" } it { should_not include "Joe Shmo" } end let() gotcha
  27. spec/user_spec.rb describe User, ".admin_names" do let(:admin) { User.create!(:admin => true,

    :first => "Clark", :last => "Kent")} let(:avg_joe) { User.create!(:admin => false, :first => "Joe", :last => "Shmo")} subject { User.admin_names } before { admin && avg_joe } ! it { should include "Clark Kent" } it { should_not include "Joe Shmo" } end let() work-around
  28. spec/user_spec.rb describe User, ".admin_names" do let!(:admin) { User.create!(:admin => true,

    :first => "Clark", :last => "Kent")} let!(:avg_joe) { User.create!(:admin => false, :first => "Joe", :last => "Shmo")} subject { User.admin_names } ! it { should include "Clark Kent" } it { should_not include "Joe Shmo" } end let!()