Permalink
Browse files

Added RSpecs and Cucumber Features

  • Loading branch information...
arunsark committed May 15, 2011
1 parent a14e73c commit 84077905750073254884164c5c6eada99940ab6a
View
@@ -33,7 +33,7 @@ gem 'devise'
# and rake tasks are available in development mode:
group :development, :test do
gem 'rspec-rails'
-# gem 'cucumber-rails'
+ gem 'cucumber-rails'
gem 'launchy'
gem 'capybara'
gem 'database_cleaner'
View
@@ -46,6 +46,16 @@ GEM
childprocess (0.1.8)
ffi (~> 1.0.6)
configuration (1.2.0)
+ cucumber (0.10.2)
+ builder (>= 2.1.2)
+ diff-lcs (>= 1.1.2)
+ gherkin (>= 2.3.5)
+ json (>= 1.4.6)
+ term-ansicolor (>= 1.0.5)
+ cucumber-rails (0.4.1)
+ cucumber (>= 0.10.1)
+ nokogiri (>= 1.4.4)
+ rack-test (>= 0.5.7)
culerity (0.2.15)
database_cleaner (0.6.7)
devise (1.2.1)
@@ -65,10 +75,13 @@ GEM
actionpack (>= 2.3.7)
activesupport (>= 2.3.7)
i18n (~> 0.4)
+ gherkin (2.3.7)
+ json (>= 1.4.6)
i18n (0.5.0)
jquery-rails (0.2.7)
rails (~> 3.0)
thor (~> 0.14.4)
+ json (1.5.1)
json_pure (1.5.1)
launchy (0.4.0)
configuration (>= 0.0.5)
@@ -134,6 +147,7 @@ GEM
json_pure
rubyzip
stringex (1.2.1)
+ term-ansicolor (1.0.5)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
@@ -150,6 +164,7 @@ PLATFORMS
DEPENDENCIES
bson_ext
capybara
+ cucumber-rails
database_cleaner
devise
factory_girl_rails
@@ -4,12 +4,16 @@ def create
begin
@post = Post.find_by_slug(params[:post_id])
@comment = @post.comments.new(params[:comment])
- @comment.save!
- redirect_to @post, :notice => "Thanks for the comment"
- rescue Exception => e
- logger.debug "Comment creation error #{e.inspect}"
- flash[:alert] = "Please fill in all the data"
- render 'posts/show'
+
+ begin
+ @comment.save!
+ logger.debug "Comment created successfully"
+ redirect_to @post, :notice => "Thanks for the comment"
+ rescue Exception => e
+ flash[:alert] = "Please fill in all the data"
+ logger.debug "Comment creation error #{e.inspect}"
+ render 'posts/show'
+ end
end
end
@@ -49,16 +49,15 @@ def create
end
logger.debug "Going to save #{Rails.logger.level} #{@post.title.inspect} #{params[:post]}"
respond_to do |format|
- begin
- @post.safely.save!
- logger.debug "Saved successfully #{@post.title.inspect}"
- format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
- format.xml { render :xml => @post, :status => :created, :location => @post }
- rescue Exception => e
- logger.debug "Could not save #{@post.title.inspect}"
- flash[:alert] = "Slug cannot be created"
- format.html { render 'new' }
- format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
+ if @post.save_post?
+ logger.debug "Saved successfully #{@post.title.inspect}"
+ format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
+ format.xml { render :xml => @post, :status => :created, :location => @post }
+ else
+ logger.debug "Could not save #{@post.title.inspect}"
+ flash[:alert] = "Slug cannot be created"
+ format.html { render 'new' }
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
@@ -1,2 +1,6 @@
module PostsHelper
+
+ def PostsHelper.generate_slug(title)
+ title.downcase.gsub(/[^[:alnum:]]/,'-').gsub(/-{2,}/,'-')
+ end
end
View
@@ -37,13 +37,23 @@ def authors
end
end
+ def save_post?
+ begin
+ safely.save!
+ true
+ rescue Exception => e
+ false
+ end
+ end
+
private
def set_published_on
self.published_on = DateTime.now
end
def generate_slug
- self.slug = self.title.downcase.gsub(/[^[:alnum:]]/,'-').gsub(/-{2,}/,'-')
+ self.slug = PostsHelper.generate_slug(self.title)
+ #self.title.downcase.gsub(/[^[:alnum:]]/,'-').gsub(/-{2,}/,'-')
end
def generate_tags
View
@@ -9,4 +9,6 @@ class User
field :first_name
field :last_name
field :nick_name
+
+ validates_presence_of :first_name, :last_name, :nick_name
end
@@ -1,4 +1,4 @@
-<% form_for [@post,@comment] do |f| %>
+<%= form_for [@post,@comment] do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this post from being saved:</h2>
@@ -0,0 +1,18 @@
+
+Feature: Create comment
+ As a reader of the blog
+ I want to Open a Post in the Blog
+ And I should be able to fill in my comments for the post
+ So that my Comments for the Post appears in the Blog
+
+
+ Scenario: Create a Comment
+ Given a blog post with title "Foo Post" and content "Bar Content"
+ When I visit the Blog Post
+ And I fill up Author as "Arun"
+ And I fill up Email as "arun.vydianathan@gmail.com"
+ And I fill up the Content as "Excellent Post"
+ And I submit the Comment
+ Then I should see message "Thanks for the comment"
+ And I should see my Comment appearing in the Post
+
@@ -0,0 +1,24 @@
+
+Feature: Create a Post
+ As a registered user
+ I want to Create a Post in the Blog
+ So that Post appears in the Blog for others to consume
+
+
+ Scenario: Create a Post
+ Given I am a registered User with name "Arun", email "arun.vydianathan@gmail.com" and password "foobar"
+ And I sign in as "arun.vydianathan@gmail.com/foobar"
+ When I visit Create Post Page
+ And I fill up Title as "Foo Post"
+ And I fill up Content as "Bar Contents for Foo Post"
+ And I publish the Post
+ Then I should see message "Post was successfully created."
+ And I should see post in the index page
+
+
+ Scenario: Create a Post without sign in
+ Given I have not signed into the system
+ When I visit Create Post Page
+ Then I should be taken to the sign in page
+
+
@@ -0,0 +1,30 @@
+Given /^a blog post with title "([^"]*)" and content "([^"]*)"$/ do |title, content|
+ @post = Post.create!(:title => title, :content => content)
+end
+
+When /^I visit the Blog Post$/ do
+ visit post_path(@post)
+end
+
+When /^I fill up Author as "([^"]*)"$/ do |name|
+ @author = name
+ fill_in "Author", :with => name
+end
+
+When /^I fill up Email as "([^"]*)"$/ do |email|
+ fill_in "Email", :with => email
+end
+
+When /^I fill up the Content as "([^"]*)"$/ do |content|
+ @content = content
+ fill_in "Content", :with => content
+end
+
+When /^I submit the Comment$/ do
+ click_button "Create Comment"
+end
+
+Then /^I should see my Comment appearing in the Post$/ do
+ page.should have_content(@author)
+ page.should have_content(@content)
+end
@@ -0,0 +1,48 @@
+Given /^I am a registered User with name "([^"]*)", email "([^"]*)" and password "([^"]*)"$/ do |name, email, password|
+ @user = User.create!(:email => email,
+ :password => password,
+ :password_confirmation => password,
+ :first_name => name,
+ :last_name => name,
+ :nick_name => name)
+end
+
+Given /^I sign in as "(.*)\/(.*)"$/ do |email, password|
+ Given %{I go to the sign in page}
+ And %{I fill in "user_email" with "#{email}"}
+ And %{I fill in "user_password" with "#{password}"}
+ And %{I press "Sign in"}
+end
+
+When /^I visit Create Post Page$/ do
+ visit posts_path
+ click_link 'New Post'
+end
+
+When /^I fill up Title as "([^"]*)"$/ do |title|
+ @title = title
+ fill_in "Title", :with => title
+end
+
+When /^I fill up Content as "([^"]*)"$/ do |content|
+ fill_in "Content", :with => content
+end
+
+When /^I publish the Post$/ do
+ click_button "Create Post"
+end
+
+Then /^I should see post in the index page$/ do
+ post = Post.find_by_slug(PostsHelper.generate_slug(@title))
+ page.should have_content(@title)
+ page.should have_content("Comment")
+end
+
+
+Given /^I have not signed into the system$/ do
+
+end
+
+Then /^I should be taken to the sign in page$/ do
+ page.should have_content("Sign in")
+end
@@ -209,3 +209,8 @@ def with_scope(locator)
Then /^show me the page$/ do
save_and_open_page
end
+
+Then /^I should see message "([^"]*)"$/ do |msg|
+ page.should have_content(msg)
+end
+
View
@@ -32,7 +32,8 @@
# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin
- DatabaseCleaner.strategy = :transaction
+ DatabaseCleaner.orm = 'mongoid'
+ DatabaseCleaner.strategy = :truncation
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
@@ -11,6 +11,12 @@ def path_to(page_name)
when /the home\s?page/
'/'
+ when /the sign up page/
+ '/users/sign_up'
+
+ when /the sign in page/
+ '/users/sign_in'
+
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+
+describe CommentsController do
+
+ let (:comment) { mock_model(Comment).as_null_object }
+ before(:each) do
+ @post = Factory(:post)
+ @attr = { :author => "Arun", :email => "arun@gmail.com", :content => "Excellent Post!"}
+ Post.stub(:find_by_slug).with("foo-post").and_return(@post)
+ @post.comments.stub(:new).and_return(comment)
+ end
+
+ context "#success" do
+ before(:each) do
+ comment.stub(:save!)
+ end
+
+ it "should create a comment" do
+ comment.should_receive(:save!)
+ post :create , :post_id=>@post.slug,:comment=> @attr
+ flash[:notice].should =~ /thanks for the comment/i
+ response.should redirect_to(post_path(@post))
+ end
+ end
+
+ context "#failure" do
+ before(:each) do
+ comment.stub(:save!).and_raise
+ end
+
+ it "should not create a comment" do
+ comment.should_receive(:save!).and_raise
+ post :create , :post_id=>@post.slug,:comment=> @attr
+ flash[:alert].should =~ /please fill in all the data/i
+ response.should render_template('posts/show')
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 8407790

Please sign in to comment.