gpt4 book ai didi

ruby-on-rails-3 - Rspec 测试失败时说 flash 消息不存在

转载 作者:行者123 更新时间:2023-12-05 01:29:24 25 4
gpt4 key购买 nike

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

一切正常。

这是 Controller :

 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.

如果我在 Controller 中设置 flash[:success],为什么它说它得到了 nil?

最佳答案

对于 JS 案例,尝试使用 flash.now[:success] 而不是 flash[:success]

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

关于ruby-on-rails-3 - Rspec 测试失败时说 flash 消息不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6489521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com