Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .F....
- 1)
- 'CommentsController CommentsController handling create a comment is invalid should render the posts show template' FAILED
- Expected template "posts/show" to be rendered, got no render
- ./spec/controllers/comments_controller_spec.rb:64:
- ------
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
- describe CommentsController do
- describe CommentsController, " handling create" do
- before(:each) do
- @comment = mock_model(Comment).as_null_object
- @post = mock_model(Post, :save => nil).as_null_object
- @post.comments.stub!(:build).and_return(@comment)
- Post.stub!(:find_by_permalink).and_return(@post)
- @params = {'author_name' => 'Joe User', 'author_email' => 'joe@user.org', 'body' => 'My comment!'}
- end
- def post_with_valid_comment
- post :create, :comment => {'author_name' => 'Joe User', 'author_email' => 'joe@user.org', 'body' => 'My comment!'}
- end
- def post_with_invalid_comment
- post :create, :comment => {'author_email' => 'joe@user.org', 'body' => 'My comment!'}
- end
- it "should build a new comment" do
- @post.comments.should_receive(:build).with(@params).and_return(@comment)
- post_with_valid_comment
- end
- context "a comment is valid" do
- it "should save the comment" do
- @comment.should_receive(:save).and_return(true)
- post_with_valid_comment
- end
- it "should set the flash message" do
- post_with_valid_comment
- flash[:notice].should == 'Comment successfully added!'
- end
- it "should redirect back to the post page" do
- post_with_valid_comment
- response.should redirect_to(post_path(@post))
- end
- end
- context "a comment is invalid" do
- it "should not save the comment" do
- @comment.should_receive(:save).and_return(false)
- post_with_invalid_comment
- end
- it "should render the posts show template" do
- post_with_invalid_comment
- response.should render_template('posts/show')
- end
- end
- end
- end
- --------------------
- class CommentsController < ApplicationController
- before_filter :set_post
- def create
- @comment = @post.comments.build(params[:comment])
- if @comment.save
- flash[:notice] = 'Comment successfully added!'
- redirect_to post_path(@post)
- else
- render :template => 'posts/show'
- end
- end
- private
- def set_post
- @post = Post.find_by_permalink(params[:post_id])
- end
- end
Add Comment
Please, Sign In to add comment