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](http://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](http://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:
‘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