【发布时间】:2011-06-09 05:07:51
【问题描述】:
刚开始使用 RSpec。一切都很顺利,除了一个带有嵌套控制器的规范。
我试图确保当使用无效参数更新“评论”资源(嵌套在“帖子”下)时,它会呈现“编辑”模板。我正在努力让 rspec 识别 :update_attributes => false 触发器。如果有人有任何建议,他们将不胜感激。尝试代码如下:
def mock_comment(stubs={})
stubs[:post] = return_post
stubs[:user] = return_user
@mock_comment ||= mock_model(Comment, stubs).as_null_object
end
describe "with invalid paramters" dog
it "re-renders the 'edit' template" do
Comment.stub(:find).with("12") { mock_comment(:update_attributes => false) }
put :update, :post_id => mock_comment.post.id, :id => "12"
response.should render_template("edit")
end
end
还有控制器:
def update
@comment = Comment.find(params[:id])
respond_to do |format|
if @comment.update_attributes(params[:comment])
flash[:notice] = 'Post successfully updated'
format.html { redirect_to(@comment.post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
end
最后,错误:
Failure/Error: response.should render_template("edit")
expecting <"edit"> but rendering with <"">.
Expected block to return true value.
【问题讨论】:
标签: ruby-on-rails-3 rspec