Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated README and added steps wrappers

  • Loading branch information...
commit bb204bb8d9b76c27c649ef29fb73df120881f1ba 1 parent b3a55a2
@soveran soveran authored
Showing with 88 additions and 7 deletions.
  1. +66 −5 README.markdown
  2. +19 −2 lib/stories/runner.rb
  3. +3 −0  test/all_test.rb
View
71 README.markdown
@@ -32,22 +32,83 @@ If you are using Rails, you can use stories for your integration tests with Webr
class UserStoriesTest < ActionController::IntegrationTest
story "As a user I want to log in so that I can access restricted features" do
setup do
- @user = User.spawn
+ @user = User.spawn :name => "Albert", :email => "albert@example.com"
end
scenario "Using good information" do
visit "/"
click_link "Log in"
- fill_in :session_email, :with => user.email
- fill_in :session_password, :with => user.password
+ fill_in "Email", :with => user.email
+ fill_in "Password", :with => user.password
click_button "Sign in"
- assert_not_contain "Log in"
- assert_not_contain "Sign up"
+ assert_contain "Logout"
+ assert_contain "Welcome Albert"
end
end
end
+This will produce the following output:
+
+ - As a user I want to lo in so that I can access restricted features
+ Using good information
+ Go to “/”
+ Click “Log in”
+ Fill in “Email” with “albert@example.com”
+ Fill in “Password” with “secret”
+ Click “Sign in”
+ I should see “Logout”
+ I should see “Welcome Albert”
+
+Custom reports
+--------------
+
+Stories ships with reports for many Webrat helpers, but you can write your own version or add reports
+for other helpers.
+
+For instance, let's say you add this assertion:
+
+ module Test::Unit::Assertions
+ def assert_current_page(path)
+ assert_equal path, current_url.sub(%r{^http://www\.example\.com}, '')
+ end
+ end
+
+When you run it, no output will be generated because Stories doesn't know what to say about this assertion.
+Adding a custom report is easy:
+
+ module Stories::Webrat
+ report_for :assert_current_page do |page|
+ "I should be on #{quote(page)}"
+ end
+ end
+
+All this should reside in your `stories_helper.rb`.
+
+Step wrappers
+---------------
+
+There are two other methods that are useful for reporting steps or assertions: `silent` and `report`.
+
+The `silent` helper gives you the ability to suspend the steps and assertions' normal output:
+
+ ...
+ silent do
+ assert_contain "Some obscure hash"
+ end
+ ...
+
+In a similar fashion, `report` lets you replace the normal output with your own message:
+
+ ...
+ report "I verify that the hash generated correctly" do
+ assert_contain "The same obscure hash"
+ end
+ ...
+
+Running stories
+---------------
+
You can run it normally, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:
$ ruby my_test.rb -n test_yet_more_tests
View
21 lib/stories/runner.rb
@@ -36,6 +36,23 @@ def scenario(name, &block)
end
end
+module Test::Unit::Assertions
+ def report(text, &block)
+ @scenario.steps << text
+ silent(&block) if block_given?
+ end
+
+ def silent(&block)
+ scenario, @scenario = @scenario, Stories::Scenario.new("#{@scenario.name} (Silent)")
+
+ begin
+ block.call
+ ensure
+ @scenario = scenario
+ end
+ end
+end
+
module Stories
class Story
attr_accessor :name, :scenarios
@@ -126,7 +143,7 @@ module Stories::Webrat
report_for :click_link do |name|
"Click #{quote(name)}"
end
-
+
report_for :click_button do |name|
"Click #{quote(name)}"
end
@@ -138,7 +155,7 @@ module Stories::Webrat
report_for :visit do |page|
"Go to #{quote(page)}"
end
-
+
report_for :check do |name|
"Check #{quote(name)}"
end
View
3  test/all_test.rb
@@ -21,6 +21,9 @@ def some_helper
end
scenario "A call to a helper" do
+ report "I use some helper" do
+ some_helper
+ end
assert_equal 1, some_helper
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.