0

我正在尝试通过 rspec 运行一些测试,并且我不断收到未重新路由的流量的错误。它特别发生在 PUT 和 DELETE 请求上。我没有收到路由错误,错误是:

Expected response to be a redirect to http://www.example.com/ but was a redirect to https://www.example.com/users/212

我的 UsersController 看起来有点像这样:

class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:edit, :update, :index, :destroy]

  .
  .
  .

  def update
    puts "Update called."

    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end

然而,在运行 rspec 时从不打印“调用的更新”,但在 show 方法中放置的“调用的显示”总是如此。signed_in 方法如下所示:

def signed_in_user
  puts "Signed in?: #{signed_in?}"

  if !signed_in?

    store_location
    flash[ :notice ] = "You must sign in to access this page."
    redirect_to signin_url
  end
end

“已登录”也从不打印。

这是引发错误之一的测试:

describe "for non-signed-in users" do
  let(:user) { FactoryGirl.create(:user) }

  describe "in the Users controller" do
    describe "submitting to the update action" do
      before { put user_path(user) }
      specify { response.should redirect_to(signin_path) }
    end
  end
end

我最好的猜测是测试不尊重之前调用中的“put”关键字,但我不知道为什么,或者那里发生了什么。我正在使用 rails 3.2.13 版和 rspec-rails 2.11.0 版

编辑:如果我puts user在测试语句的开头插入(但在 let 语句之后),我会收到一个错误:

undefined local variable or method `user' for #<Class:0x007fd4943743d8>

4

1 回答 1

0

如果有人好奇,我会发现我在某些方面遇到的错误,尽管更好的解释会很棒。

基本上我是在强迫 Rails 应用程序使用 ssl,出于某种原因,rspec 真的不喜欢那样。注释掉 ssl 行解决了所有问题。

于 2013-05-17T19:29:38.187 回答