Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Redirect_to on Rspec

rspec

  • Please log in to reply
3 replies to this topic

#1 Jakson Rochelly

Jakson Rochelly

    Passenger

  • Members
  • 6 posts
  • LocationPalmas, TO - Brazil

Posted 29 August 2013 - 01:51 AM

Hi guys, I'm trying to test it I get redirect when I pass a object with nil field and I'm getting the error:

Failure/Error: }.to assert_redirected_to close_path(@call)
     MiniTest::Assertion:
       Expected response to be a <redirect>, but was <200>

Here's what I did:

describe "#Close" do
    before(:each) { @call = FactoryGirl.create(:call) }


    it 'successful closes a call' do
      get :close, {:id => @call.to_param}, valid_session
      response.status.should eq 200
    end


    it "redirects to #close if there's no service described" do
      expect {
        put :done, {:id => @call.to_param, :call => FactoryGirl.attributes_for(:call, {:service => nil})}, valid_session
      }.to assert_redirected_to close_path(@call)
    end
  end

The controller:

# GET /calls/1/close
  def close
    @call = Call.find(params[:id])
  end


  # POST /calls/done
  def done
    respond_to do |format|
      if @call.update(call_params) && @call.service != nil
        format.html { redirect_to @call, notice: 'Chamada ##{@call.id} foi fechado.' }
        format.json { head :no_content }
      else
        format.html { redirect_to :action => :close, :id => @call.id }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

Why it's always getting 200? Does anyone know??

 



#2 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 29 August 2013 - 04:46 AM

Can you try something like 

describe "#close if there's no service described" do
  before { ..... do your stuff here ... } 
  specify { expect(response).to redirect_to(close_path) }
end


#3 Ohm

Ohm

    Guard

  • Members
  • 186 posts
  • LocationCopenhagen

Posted 29 August 2013 - 05:10 AM

Shouldn't 

 

it "redirects to #close if there's no service described" do
  expect {
    put :done, {:id => @call.to_param, :call => FactoryGirl.attributes_for(:call, {:service => nil})}, valid_session
  }.to assert_redirected_to close_path(@call)
end

 

be more along the lines of

it "redirects to #close if there's no service described" do
  put :done, {:id => @call.to_param, :call => FactoryGirl.attributes_for(:call, :service => nil)}, valid_session
  response.should_be redirected
end

Blog: http://ohm.sh | Twitter: madsohm


#4 Jakson Rochelly

Jakson Rochelly

    Passenger

  • Members
  • 6 posts
  • LocationPalmas, TO - Brazil

Posted 30 August 2013 - 02:26 AM   Best Answer

Thank you guys! Your answers were very helpful!







Also tagged with one or more of these keywords: rspec

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users