Permalink
Please sign in to comment.
Showing
with
456 additions
and 20 deletions.
- +1 −1 Gemfile
- +15 −0 Gemfile.lock
- +10 −6 app/controllers/comments_controller.rb
- +9 −10 app/controllers/posts_controller.rb
- +4 −0 app/helpers/posts_helper.rb
- +11 −1 app/models/post.rb
- +2 −0 app/models/user.rb
- +1 −1 app/views/comments/_new.html.erb
- +18 −0 features/create_comment.feature
- +24 −0 features/create_post.feature
- +30 −0 features/step_definitions/comment_steps.rb
- +48 −0 features/step_definitions/post_steps.rb
- +5 −0 features/step_definitions/web_steps.rb
- +2 −1 features/support/env.rb
- +6 −0 features/support/paths.rb
- +39 −0 spec/controllers/comments_controller_spec.rb
- +65 −0 spec/controllers/posts_controller_spec.rb
- +26 −0 spec/factories.rb
- +47 −0 spec/models/comment_spec.rb
- +65 −0 spec/models/post_spec.rb
- +28 −0 spec/models/user_spec.rb
15
Gemfile.lock
@@ -1,2 +1,6 @@ | ||
module PostsHelper | ||
+ | ||
+ def PostsHelper.generate_slug(title) | ||
+ title.downcase.gsub(/[^[:alnum:]]/,'-').gsub(/-{2,}/,'-') | ||
+ end | ||
end |
@@ -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 |
@@ -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