sábado, 27 de julio de 2013

How to write user stories rspec and cucumber

First of all, what we need to know what are the components of a story, well, there are: a title, a narrative and acceptance criteria
Cucumber
In cucumber you have to write features so:
First you have to create a file with the extension .feature
In there you keep this format:
Feature: Name of the feature
   As a ….
   I want to ….
   So I can …
Note: you can also do it just writing a paragraph explaining what is that feature about.
After that you need to write the Scenarios, so basically the format is:
Scenario: name of the scenario
    Given ……
    When  ...….
    Then …….
And where the points are you just replace for text where you explain what is going to happen. For example in The RSpec book in the first part they make a code braker game so the first feature and scenario that they describe is starts game and they do it like this:
Feature: code-breaker starts game

  As a code-breaker
  I want to start a game
  So that I can break the code

  Scenario: start game
    Given I am not yet playing
    When I start a new game
    Then I should see "Welcome to Codebreaker!"
    And I should see "Enter guess:"

After writing our feature now we need to write the steps, because if we run our feature like that it will give us some warnings and errors. So we write our steps in another file called   xxx_steps.rb like this:
Given /^I am not yet playing$/ do
end

When /^I start a new game$/ do
  game = Codebreaker::Game.new(output)
  game.start('1234')
end

Then /^I should see "([^"]*)"$/ do |message|
  output.messages.should include(message)
end

Where basically if you can notice the format is just
Given/When or Then followed by /^What we write in the scenario$/  do
call the methods
end

So with that we’re done with cucumber.
Let’s see how we do it in rspec, here what we do is describe:
describe "class name" do
    describe "what I’m testing" do
      context "which is the case" do
        it "what it does" do
          marker = Marker.new('1234','5555')
          #"also if I add  pending "What it's pending"" (so in this way the line after pending is not run)
          marker.exact_match_count.should == 0
        end
      end

     describe "what I’m testing" do
      context "which is the case" do
        it "what it does" do
          marker = Marker.new('1234','5555')
          #"Also if I add pending "What it's pending""
          marker.exact_match_count.should == 0
        end
      end
                .
                .
                .
     end
                .
                .
                .
     describe "what I’m testing" do
      context "which is the case" do
        it "what it does" do
          marker = Marker.new('1234','5555')
          #"Also if I add pending "What it's pending""
          marker.exact_match_count.should == 0
        end
      end
     end
end

So in that way after we describe each scenario and all if we run the spec everything just says that things are not yet implemented, we are ready to the next step that is write the logic.

Bibliography



Chelimsky, D. (2010). The RSpec Book. The Pragmatic Bookshelf.

No hay comentarios:

Publicar un comentario