ruby-on-rails - Rspec 上的 Redirect_to

标签 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

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

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

最佳答案

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

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

要修复,首先在#done中找到@call,比如

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

关于ruby-on-rails - Rspec 上的 Redirect_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19006578/

相关文章:

javascript - 为什么我的 JSON 数据作为数据属性从 erb 传递到 Javascript 时会丢失?

ruby-on-rails - 在 Ruby on Rails 中将数据从一个 Controller 提交到另一个 Controller 模型

ruby-on-rails - 简单表单输入类型

ruby-on-rails - 在单元测试中使用 HABTM Fixture 的 Rails 单表继承返回 NoMethodError : undefined method `singularize'

ruby - Chrome 中缺少 session 过期值,但在 Firefox 和 IE 中存在

ruby-on-rails - 在 RSpec Rails 中测试 HTTPS (SSL) 请求

php - Ruby 将对象实例作为参数传递

ruby-on-rails - 这些奇怪的字符是什么意思?

ruby-on-rails - Rubymine 和运行 rspec "rspec-core is not part of the bundle"

ruby - 在 Aruba 中测试 CLI 时如何解决 Cucumber::Ambiguous 匹配错误