关于Rspec的Redirect_to

时间:2013-09-25 13:40:23

标签: ruby-on-rails ruby rspec

我正在尝试测试它当我使用nil字段传递一个对象时我得到重定向并且我收到了错误:

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

这就是我的所作所为:

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

控制器:

# 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

为什么总是200?有谁知道??

2 个答案:

答案 0 :(得分:1)

如果问题#done涵盖了您应用中的完整代码,则预计会产生200的响应。

#done中,代码无效。它指示如何使用@call进行响应,但根本没有创建@call :)所以,没有任何反应,200返回。

要解决此问题,请先在@call中找到#done,例如

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

答案 1 :(得分:0)

尝试做这样的事情

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