Guest User

Untitled

a guest
Jul 10th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. .F....
  2.  
  3. 1)
  4. 'CommentsController CommentsController handling create a comment is invalid should render the posts show template' FAILED
  5. Expected template "posts/show" to be rendered, got no render
  6. ./spec/controllers/comments_controller_spec.rb:64:
  7.  
  8. ------
  9.  
  10. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  11.  
  12. describe CommentsController do
  13. describe CommentsController, " handling create" do
  14.  
  15. before(:each) do
  16. @comment = mock_model(Comment).as_null_object
  17.  
  18. @post = mock_model(Post, :save => nil).as_null_object
  19. @post.comments.stub!(:build).and_return(@comment)
  20. Post.stub!(:find_by_permalink).and_return(@post)
  21.  
  22. @params = {'author_name' => 'Joe User', 'author_email' => 'joe@user.org', 'body' => 'My comment!'}
  23. end
  24.  
  25.  
  26. def post_with_valid_comment
  27. post :create, :comment => {'author_name' => 'Joe User', 'author_email' => 'joe@user.org', 'body' => 'My comment!'}
  28. end
  29.  
  30.  
  31. def post_with_invalid_comment
  32. post :create, :comment => {'author_email' => 'joe@user.org', 'body' => 'My comment!'}
  33. end
  34.  
  35.  
  36. it "should build a new comment" do
  37. @post.comments.should_receive(:build).with(@params).and_return(@comment)
  38. post_with_valid_comment
  39. end
  40.  
  41.  
  42. context "a comment is valid" do
  43.  
  44. it "should save the comment" do
  45. @comment.should_receive(:save).and_return(true)
  46. post_with_valid_comment
  47. end
  48.  
  49.  
  50. it "should set the flash message" do
  51. post_with_valid_comment
  52. flash[:notice].should == 'Comment successfully added!'
  53. end
  54.  
  55.  
  56. it "should redirect back to the post page" do
  57. post_with_valid_comment
  58. response.should redirect_to(post_path(@post))
  59. end
  60. end
  61.  
  62.  
  63. context "a comment is invalid" do
  64.  
  65. it "should not save the comment" do
  66. @comment.should_receive(:save).and_return(false)
  67. post_with_invalid_comment
  68. end
  69.  
  70.  
  71. it "should render the posts show template" do
  72. post_with_invalid_comment
  73. response.should render_template('posts/show')
  74. end
  75. end
  76.  
  77. end
  78. end
  79.  
  80.  
  81.  
  82. --------------------
  83.  
  84.  
  85. class CommentsController < ApplicationController
  86.  
  87. before_filter :set_post
  88.  
  89. def create
  90. @comment = @post.comments.build(params[:comment])
  91.  
  92. if @comment.save
  93. flash[:notice] = 'Comment successfully added!'
  94. redirect_to post_path(@post)
  95. else
  96. render :template => 'posts/show'
  97. end
  98. end
  99.  
  100.  
  101.  
  102. private
  103.  
  104.  
  105. def set_post
  106. @post = Post.find_by_permalink(params[:post_id])
  107. end
  108. end
Add Comment
Please, Sign In to add comment