Forum: Ruby on Rails rspec to xhr

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
huang zhimin (Guest)
on 2009-05-26 17:55
(Received via mailing list)
when i want to test an ajax action, it always can't passed. The action
code
is:

  # POST /posts/:id/comments
  def create
    @comment = @post.comments.build(params[:comment])

    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      render :update do |page|
        page.insert_html :bottom, 'comments', :partial =>
'posts/comment',
:object => @comment
        page.replace_html 'comment', :partial => 'comment', :object =>
Comment.new
      end
    else
      render :update do |page|
        page.replace_html 'comment', :partial => 'comment', :object =>
@comment
      end
    end
  end

and the test code is:

  describe "handling POST /posts/:id/comments" do

    before(:each) do
      @post = Post.new
      @comment = Comment.new
      Post.stubs(:find).returns(@post)
      @post.comments.stubs(:build).returns(@comment)
    end

    it "with successful save" do
      @comment.expects(:save).returns(true)
      xhr :post, :create, :post_id => @post.id, :comment => {}
      response.should have_rjs(:insert_html, :bottom, 'comments')
      response.should have_rjs(:replace_html, 'comment')
    end

    it "with failed save" do
      @comment.expects(:save).returns(false)
      xhr :post, :create, :post_id => @post.id, :comment => {}
      response.should have_rjs(:replace_html, 'comment')
    end
  end

The result is:
1)
'CommentsController handling DELETE /comments/:id with successful
destroy'
FAILED
No RJS statement that removes 'comment_' was rendered.
./spec/controllers/comments_controller_spec.rb:38:

2)
'CommentsController handling POST /posts/:id/comments with successful
save'
FAILED
bottom.
./spec/controllers/comments_controller_spec.rb:16:

But I can do it successfully by browser. So I think there's something
wrong
with my rspec codes. Can anyone help me? Thanks in advanced.


Best Regard,
Richard

--
My home: http://www.huangzhimin.com
This topic is locked and can not be replied to.