Rspec Test Failing Says flash message not present when it is
I have a form that is submtted using ajax.
This all works.
Here is the 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
Here is the user_controller_spec test:
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
Here is the message given by the test when it fails:
1) UsersController Ajax 'create_bp' success should s开发者_StackOverflow社区how 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.
Why does it say it got nil if I am setting the flash[:success] right there in the controller?
Try use flash.now[:success] instead just flash[:success] for JS case.
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
精彩评论