【发布时间】:2013-05-17 15:23:59
【问题描述】:
我正在尝试通过 rspec 运行一些测试,但我不断收到未重新路由流量的错误。它特别发生在 PUT 和 DELETE 请求上。我没有收到路由错误,错误是:
Expected response to be a redirect to @987654321@ but was a redirect to @987654322@
我的 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 版
编辑:如果我在测试语句的开头(但在 let 语句之后)插入 puts user,我会收到错误:
undefined local variable or method `user' for #<Class:0x007fd4943743d8>
【问题讨论】:
-
我绝不是 RSpec 的权威,但我看到的每个示例都更像:
put :update, id: @user, user: { some: "params" } -
刚刚试过
put :update, id: user.id, user: user,不幸的是同样的错误编辑:正在查看错误的错误,这给了我一个错误的参数错误。 (预期的 URI 对象或 URI 字符串)
标签: ruby-on-rails rspec ruby-on-rails-3.2 rspec-rails put