[rspec-users] Trying to test flash messaging with rspec
James Martin
jimmymartin at gmail.com
Thu Mar 10 20:27:52 EST 2011
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?
On Fri, Mar 11, 2011 at 10:06 AM, Tyrel R. <lists at ruby-forum.com> wrote:
> Hello every one,
>
> So my this rspec test fails:
>
> [code]
> it "should have a welcome message" do
> post :create, :user => @attr
> response.flash[:success].should eql("Welcome new user!")
> end
> [/code]
>
> 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:
> [code]
> 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
> [/code]
>
> And the view displays with:
> [code]
> <%= 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
> [/code]
>
> Like I said the flash message works just fine however the test fails any
> ideas would be greatly appreciated.
>
> Hope all is well :)
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110311/d0e136db/attachment.html>
More information about the rspec-users
mailing list