Rspec Test Failing表示闪存消息不存在

时间:2011-06-27 06:54:34

标签: ruby-on-rails-3 rspec

我有一个使用ajax提交的表单。

这一切都有效。

这是控制器:

 def create_bp
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.save
        flash[:success] = "Your friend's details have been saved."
         #format.html { render :template => 'pages/home'}
         format.js   { render :action => "success" }
      else
        format.html { render :template => 'pages/home'}
        format.js   { render :action => "failure" }
      end
    end
  end

这是user_controller_spec测试:

   it "should show the correct flash message" do
            xhr :post, :create_bp, :user => @attr
            flash[:success].should =~ /Your friend's details have been saved./i
          end

以下是测试失败时给出的消息:

  1) UsersController Ajax 'create_bp' success should show the correct flash message
     Failure/Error: flash[:success].should =~ /Your friend's details have been saved./i
       expected: /Your friend's details have been saved./i
            got: nil (using =~)
     # ./spec/controllers/user_controller_spec.rb:77:in `block (4 levels) in <top (required)>'

The flash message is being created and I can see it on the page as well. 

如果我在控制器中设置flash [:success],为什么会说它没有结果?

1 个答案:

答案 0 :(得分:0)

尝试使用 flash.now [:success] 而不是 flash [:success] 来处理JS案例。

def create_bp
  @user = User.new(params[:user])
  respond_to do |format|
    if @user.save
      flash.now[:success] = "Your friend's details have been saved."
      format.js   { render :action => "success" }
    else
      format.html { render :template => 'pages/home'}
      format.js   { render :action => "failure" }
    end
  end
end