Writting effective specs
Upcoming SlideShare
Loading in...5
×
 

Writting effective specs

on

  • 59 views

 

Statistics

Views

Total Views
59
Slideshare-icon Views on SlideShare
59
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Writting effective specs Writting effective specs Presentation Transcript

    • Writing Specs Effectively - Swapnil Abnave
    • Rspec Wiki RSpec is a Behaviour-Driven Development tool for Ruby programmers.
    • Rspec Wiki (continued) BDD is an approach to software development that combines Test- Driven Development and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.
    • Questions ? ● where to start ? ● what to test ? ● what not to test ? ● how much to test in one go ? ● What should be the focus ? ● Test fails (how to fix broken test)
    • Where to start from ? 1. Focus on Scenario 2. Write Failing Step definition 3. Write Failing Example 4. Get the example pass 5. Refactor 7. Refactor Start with Cucumber (outside-in) 6. When Step is passing drop down to Rspec Repeat #2–#7 until scenario is passing Repeat #1–#7 when scenario is passing Repeat #3–#5 until step is passing
    • What should you be testing? Story: (one line describing the story) As a [role] I want to [some action] So that [business value!] “wear your TDD hat,“ -> focus on red/green/refactor, design, and documentation. “tester hat,” -> crazy edge cases
    • What not to test ? Rest of the world
    • How much to test in one go? • Test my entire story/feature ? • Keep it specific – one assert per test • One assert is easy to understand.
    • Test Focus • Tests should be reliable. • Tests should be easy to write. • Tests should be easy to understand.
    • Fixing broken test Well, on a bright sunny day my test build fails :( But it's a good thing :) Approach with documentation for the failure.
    • How ? Let's talk about how to do it. Consider an example of 'NewsFeed' application.
    • Story: Admin creates news and clients(guests) read them. Break the story in two scenarios: ✔ News Feed : as an admin I can add a news-feed ✔ News Feed : as a guest I can read a news feed A story to tell
    • Starting with cucumber #spec/features/news_feeds_spec.rb require 'spec_helper' feature "News Feed" do context "as an admin" do scenario "I can add a news-feed" do end end context "as a guest" do scenario "I can reads a news feed" do end end end
    • News Feed : as an admin I can add a news feed context "as a admin" do scenario "I can add a news-feed" do # Setup data admin_user = create(:admin_user) sign_in(admin_user) visit root_path click_link "News" # Initial Verification # Final Expectations end end
    • News Feed : as an admin I can add a news feed (cont.) context "as a admin" do scenario "I can add a news-feed" do # Setup data ... # Initial Verification expect(page).to_not have_content "Rspec is Awesome!!!" click_link "Add News Feed" # Fill in the form fill_in "Date", with: "2014-04-15" fill_in "Title", with: "Rspec is Awesome!!!" fill_in "Body", with: "Rspec 3.0 has been released. RSpec is a Behaviour-Driven Develop tool for Ruby programmers" click_button "Create News feed" end end
    • News Feed : as an admin I can add a news feed (cont.) context "as a admin" do scenario "I can add a news-feed" do # Setup data ... # Initial Verification ... # Final Expectations expect(current_path).to eq news_feeds_path expect(page).to have_content "Successfully created news feed." expect(page).to have_content "2014-04-15: Rspec is Awesome!!!" end end
    • News Feed : as a guest I can read a news feed context "as a guest" do scenario "reads a news feed" do visit root_path click_link "News" expect(page).to_not have_content 'Add News Release' click_link "2014-04-15: Rspec is Awesome!!!" expect(page).to have_content "Rspec 3.0 has been released. RSpec is a Behaviour-Driven Development tool for Ruby programmers" end end
    • require 'spec_helper' describe NewsFeed do it { should validate_presence_of :released_on } it { should validate_presence_of :title } it { should validate_presence_of :body } context '#title_with_date' do it "returns the formatted date and title as a string" do news_feed = NewsFeed.new( released_on: '2014-04-15', title: 'Rspec 3.0 Released') expect(news_feed.title_with_date).to eq '2014-04-15: Rspec 3.0 Released' end end end Unit Test NewsFeed
    • Pass the unit test class NewsFeed < ActiveRecord::Base validates :released_on, presence: true validates :title, presence: true validates :body, presence: true def title_with_date "#{released_on.strftime('%Y-%m-%d')}: #{title}" end end
    • # Thank You