Trying to test flash messaging with rspec

Hello every one,

So my this rspec test fails:

it "should have a welcome message" do
        post :create, :user => @attr
        response.flash[:success].should eql("Welcome new user!")
      end

However when I create a new user the flash message does display just as
I had wanted it to.

The controller has this in it:

def create
    @user = User.new(params[:user])
    if @user.save
      flash[:success] = "Welcome new user!"
      redirect_to @user
    else
      @title = "Sign up"
      render 'new'
    end
  end

  def show
    @user  = User.find(params[:id])
    @title = @user.userName
  end
end

And the view displays with:

<%= flash_helper %>

The helper is:

def flash_helper
      f_names = [:success]
      fl = ''

      for name in f_names
        if flash[name]
          fl = fl + "<div class=\"notice\">#{flash[name]}</div>"
        end
      flash[name] = nil;
    end
    return fl.html_safe
  end

Like I said the flash message works just fine however the test fails any
ideas would be greatly appreciated.

Hope all is well :slight_smile:

How about just testing the flash directly, which should be accessible
from
your test:

flash[:success].should =~ /welcome new user/i

I’ve used a case insensitive regex here, which I think captures the
intent
of your test without being quite so rigid.

What do you think?

That worked perfectly thank you :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs