[rspec-users] 'new' method works as expected but rspec does not recognise flash contents or redirection

Gordon anexiole at gmail.com
Mon Mar 26 10:47:33 UTC 2012


hi all :)

 Found a spec failing today.
Not sure why.

 When I run my application and successfully added a new object, i can
see
1) a redirection has taken place to the index page as expected (302
code returned) from the console
2) the flash notice's message being displayed in the index page

BUT
when I run the specs (see below), it fails indicating that the
redirection did not occur (as a 200 code was returned) AND
that the flash notice message was nil.


---------specs error start -------------------------------------

  1) PartsController saves the new part object successfully sets the
flash with a success message
     Failure/Error: flash[:notice].should eq('Part was successfully
created.')

       expected: "Part was successfully created."
            got: nil

       (compared using ==)
     # ./spec/controllers/parts_controller_spec.rb:35:in `block (3
levels) in <top (required)>'

  2) PartsController saves the new part object successfully redirects
the user back to the parts index page
     Failure/Error: response.should redirect_to( :action => 'index' )
       Expected response to be a <:redirect>, but was <200>
     # ./spec/controllers/parts_controller_spec.rb:39:in `block (3
levels) in <top (required)>'

--------- specs error end   -------------------------------------

--------- extract of spec/controllers/parts_controller_spec.rb - start
------------------

  context 'saves the new part object successfully' do
        before(:each) do
            post :create, :part => { 'title' => 'HKS boost
controller' }
        end

        # we could combine the 2 specs below to save on the post
request
        # to the create action but having 2 separate specs would give
        # clarity
        it 'sets the flash with a success message' do
            flash[:notice].should eq('Part was successfully created.')
        end

        it 'redirects the user back to the parts index page' do
            response.should redirect_to( :action => 'index' )
        end
    end

--------- extract of spec/controllers/parts_controller_spec.rb - end
------------------


I'm not sure what's going on :(




--------  "parts_controller.rb" - start
-------------------------------

  # POST /parts
  # POST /parts.xml
  def create
    # Record current user's id as he/she created the part
    params[:part][:created_by] = current_user.id
    params[:part][:updated_by] = current_user.id

    @part = Part.new(params[:part])
    respond_to do |format|
        if @part.save
            flash[:notice] = 'Part was successfully created.'
            format.html { redirect_to(parts_path) }
            format.xml  { render :xml => @part, :status
=> :created, :location => @part }
            format.json  {
                @parts = Part.all;
                render :json => @parts
            }
        else
            format.html { render :action => "new" }
            format.xml  { render :xml => @part.errors, :status
=> :unprocessable_entity }
        end
    end
  end


--------  "parts_controller.rb" - end  -------------------------------



More information about the rspec-users mailing list