Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails flash[:注意]。不应该\u nil在rspec中失败_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails flash[:注意]。不应该\u nil在rspec中失败

Ruby on rails flash[:注意]。不应该\u nil在rspec中失败,ruby-on-rails,rspec,Ruby On Rails,Rspec,以下是控制器的rspec代码: it "should render edit if update was not saved" do item = Factory(:lease_item) session[:corp_head] = true post 'update', {:id => item.id, :name => 'nil'} flash[:notice].should_not be_nil response.should render 'edit'

以下是控制器的rspec代码:

it "should render edit if update was not saved" do
  item = Factory(:lease_item)
  session[:corp_head] = true
  post 'update', {:id => item.id, :name => 'nil'}
  flash[:notice].should_not be_nil
  response.should render 'edit'    
end
      ...........
      else
        #back to new
        flash[:notice] = "Item NOT updated"
        render 'edit'
      end
      .........
控制器中的更新是:

  def update
    if (eng? && dept_head?) || corp_head? || ceo?
      @lease_item = LeaseItem.find(params[:id])
      @lease_item.input_by_id = session[:user_id]
      if @lease_item.update_attributes(params[:lease_item], :as => :roles_update)

        #redirect
        redirect_to URI.escape("/view_handler?index=0&msg=Lease item updated successfully")
      else
        #back to new
        render 'edit', :notice => "Item NOT updated"
      end
    else
      #back to previous page
      redirect_to URI.escape("/view_handler?index=0&msg=NO right to update lease item")
    end    
  end
以下是来自rspec的错误代码:

  1) LeaseItemsController GET 'update' should render edit if update was not saved
     Failure/Error: flash[:notice].should_not be_nil
       expected: not nil
            got: nil
flash中应显示“项目未更新”。但是为什么flash没有任何功能呢?或者如何识别有一条带有呈现“编辑”的消息:notice=>“Item NOT updated”

谢谢

更新:

以下是控制器的更改:

it "should render edit if update was not saved" do
  item = Factory(:lease_item)
  session[:corp_head] = true
  post 'update', {:id => item.id, :name => 'nil'}
  flash[:notice].should_not be_nil
  response.should render 'edit'    
end
      ...........
      else
        #back to new
        flash[:notice] = "Item NOT updated"
        render 'edit'
      end
      .........
以下是通过的rspec代码:

   it "should render edit if update was not saved" do
      item = Factory(:lease_item)
      session[:corp_head] = true
      post 'update', {:id => item.id, :lease_item => {:name => 'nil'}}
      flash.should_not be_nil
      response.should render_template(:action=> "edit")  
    end

如果使用flash[:notice],它将不起作用。不应为\u nil(或..flash.now[:notice]…)。误差为零,与之前相同。此外,response.should render“edit”(或…render:action=>“edit”)也未通过。错误为NameError或NoMethodError。不知道为什么。

首先,我看不出您在代码中的
闪存设置的确切位置

不管怎样,替换:

  post 'update', {:id => item.id, :name => 'nil'}
与:

将您的else更改为:

else
  #back to new
  flash[:notice] = "Item NOT updated"
  render 'edit'
end

您正在将变量
notice
中的“Item NOT updated”(项目未更新)发送到
edit
partial,但您是否实际设置了
flash[:notice]
?如何设置flash[:notice]?没有渲染“foo”,:notice=>“blah”…阅读文档。使用flash[:notice]=“项未更新”,然后渲染“编辑”:渲染命令中包含通知。它以flash结束,不是吗?post命令和post命令都没有错误。这两个帖子之间真的有区别吗?:注意,render命令中没有。Render也不是命令…查看rspec仍然返回错误:失败/错误:flash[:注意]。\u不应该是\u nil预期的:不是nil得到的:nil真的吗?执行响应。应呈现“编辑”,然后执行flash[:注意]。不应为零。我不明白,您还有问题吗?您需要在rspec测试开始时添加渲染视图。描述LeaseItemsController是否渲染视图还可以在views/leaseitems/edit.html.haml(或erb)中创建文件