简体   繁体   English

Rails rspec控制器操作对象方法存根

[英]rails rspec controller action object method stub

I'm trying to test my controller #merge_with action. 我正在尝试测试我的控制器#merge_with动作。 I want to test that @article is not nil, and gets assigned the article found in the database. 我想测试@article不是零,并分配在数据库中找到的文章。 Not sure why the test is failing, because the action seems to work just fine in the browser. 不确定测试失败的原因,因为该操作在浏览器中似乎正常。

The controller #merge_with action. 控制器#merge_with操作。

def merge_with 
  unless current_user.admin?
    flash[:error] = _("You are not allowed to perform a merge action")
    redirect_to :action => :index
    return
  end
  @article = Article.find_by_id(params[:id])
  if @article.merge_with(params[:merge_with])
    flash[:notice] = _("Articles successfully merged!")
    redirect_to :action => :index
  else
    flash[:notice] = _("Articles couldn't be merged")
    redirect_to :action => :edit, :id => params[:id]
  end
end

My RSpec test of the redirect is failing due to the merge_with called on the @article object. 由于@article对象上调用了merge_with,我对重定向的RSpec测试失败。

context "when user is admin" do
  before :each do
    @admin_user = Factory(:user, :profile => Factory(:profile_admin))
    @article = Factory(:article, :user => @user)
    request.session = {:user => @admin_user.id}
  end

  it "should merge articles" do
    Article.should_receive(:find_by_id).with(@article.id).and_return(@article)
    post :merge_with, id: @article.id
    assigns(:article).should == @article
  end
end

The Error Message 错误讯息

expected: #<Article id: 1, type: "Article", title: "A big article", author: nil, body: "A content with several data", extended: "extended content for fun", excerpt: nil, created_at: "2013-11-22 21:19:38", updated_at: "2013-11-22 21:19:36", user_id: nil, permalink: "a-big-article", guid: "deadbeef2", text_filter_id: nil, whiteboard: nil, name: nil, published: true, allow_pings: true, allow_comments: true, published_at: "2005-01-01 02:00:00", state: "published", parent_id: nil, settings: {}, post_type: "read">
        got: nil (using ==)

I would check a few things: 我会检查一些事情:

  • Do you have any before filter that may be interfering? 您是否有可能会干扰之前的滤波器? Do "puts response.body.inspect" and check if the output of the method is not some redirect for example (actually, your "unless current_user.admin?" redirect should be a before filter, so you can reuse it for other actions that require an admin) 例如,执行“ puts response.body.inspect”并检查该方法的输出是否不是某些重定向(实际上,您的“除非current_user.admin?”重定向应该是一个前置过滤器,所以您可以将其重用于其他操作)需要管理员)

  • You can also check what's happening inside the method with some debugging, I mean, is current_user a valid admin? 您还可以通过调试来检查方法内部发生的情况,我的意思是current_user是有效的管理员吗? Do "puts current_user.inspect" before the unless, if it's valid do a "puts @article.inspect" before find_by_id, what happens if @article is not found with @article = Article.find_by_id(params[:id])? 除非在有效的情况下在find_by_id之前放入“ puts @ article.inspect”,否则是否在“ puts current_user.inspect”之前放置,如果在@article = Article.find_by_id(params [:id])找不到@article的情况下会发生什么? you have some bug there if the id is invalid cause you are calling merge_with on nil. 如果id无效,则会有一些bug,因为您在nil上调用了merge_with。

Debug that and you will end up with the answer to your error, I'm guessing its just "unless current_user.admin?" 调试一下,您将得到错误的答案,我猜它只是“除非current_user.admin?”。 redirecting you, so @article is never assigned. 重定向您,因此从未分配@article。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM