user2815524
user2815524

Reputation:

Redirect_to on Rspec

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


  # PUTS /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??

Upvotes: 2

Views: 476

Answers (2)

Billy Chan
Billy Chan

Reputation: 24815

If #done in question covered full code in your app, a response of 200 is expected.

In #done, the code dose nothing. It instructs how to respond with @call but there is no creation of @call at all :) So, nothing happens, 200 returned.

To fix, find @call at first in #done, like

def done
  @call = Call.find(params[:id]) # example code
  respond_to do |format|
  # .....

Upvotes: 1

MZaragoza
MZaragoza

Reputation: 10111

try to do something like this

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

Upvotes: 0

Related Questions