2

我正在尝试对其进行测试,当我传递一个带有 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?有人知道吗??

4

2 回答 2

1

如果#done有问题涵盖了您的应用程序中的完整代码,则预期响应为 200。

#done中,代码什么也不做。它指示如何响应,@call但根本没有创建@call:) 所以,没有任何反应,返回 200。

要修复,@call首先在 中查找#done,例如

def done
  @call = Call.find(params[:id]) # example code
  respond_to do |format|
  # .....
于 2013-09-25T14:12:53.817 回答
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
于 2013-09-25T13:45:48.057 回答