使用 before_filter 时,factory_girl rspec 测试失败

发布于 2024-12-22 10:52:36 字数 1558 浏览 0 评论 0原文

我这里有一个新手问题。我一直在遵循 Rails 教程,但在测试中遇到了障碍。当我实现以下过滤器时...

class UsersController < ApplicationController
before_filter :authenticate, :only => [:edit, :update]

...与“GET edit”和“PUT update”相关的所有 user_controller_spec.rb 测试在通过之前都开始失败。这是应该返回 true 的一个,但返回 false:

describe "GET 'edit" do

 before(:each) do
   @user = Factory(:user)
   test_sign_in(@user)
 end

 it "should be successful" do
   get :edit, :id => @user
   response.should be_success
 end
end

这是我在 spec_helper.rb 中的 test_sign_in 代码。sign_in

def test_sign_in(user)
 controller.sign_in(user)
end

方法位于 SessionsHelper 中,它包含在 ApplicationController 中:

def sign_in(user)
 cookies.permanent.signed[:remember_token] = [user.id, user.salt]
 current_user = user
end

我不知道如何进一步调查。我的猜测是,由于 Rspec 无法“获取编辑”,因此必须有一个我可以查看的请求日志,但测试只告诉我它返回了 false。接下来我可以去哪里看?

更新:我在 log/test.log 中发现,每个“Processing by UsersController#index as HTML”行后面都跟着一个重定向,如下所示:

  Processing by UsersController#index as HTML
  [1m [35mUser Load (0.2ms) [0m  SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
  Redirected to http://test.host/signin

这是否意味着每个测试索引操作后面都跟着重定向到登录?如果这是真的,那么它似乎与测试一致,包括 test_sign_in 方法(上面)失败以及其他测试(例如此测试)通过:

describe "GET 'index' for non-signed-in users" do     
  it "should deny access" do
    get :index
    response.should redirect_to(signin_path)
  end
end

我将尝试再次围绕 test_sign_in 方法进行思考。

I have a newbie question here. I've been following along with the rails tutorial and I've hit a snag in my tests. When I implement the following filter...

class UsersController < ApplicationController
before_filter :authenticate, :only => [:edit, :update]

...all of my user_controller_spec.rb tests related to 'GET edit' and 'PUT update' start to fail where before they passed. Here's one that should have returned true, but returned false:

describe "GET 'edit" do

 before(:each) do
   @user = Factory(:user)
   test_sign_in(@user)
 end

 it "should be successful" do
   get :edit, :id => @user
   response.should be_success
 end
end

And this is my test_sign_in code in spec_helper.rb

def test_sign_in(user)
 controller.sign_in(user)
end

The sign_in method is in SessionsHelper, which is incuded in ApplicationController:

def sign_in(user)
 cookies.permanent.signed[:remember_token] = [user.id, user.salt]
 current_user = user
end

I don' know how to further investigate. My guess is that since Rspec was unable to 'GET edit' there must be a log of that request that I can look at, but the test only tells me it returned false. Where can I look next?

Update: What I found in log/test.log is that every 'Processing by UsersController#index as HTML' line is followed by a redirect as follows:

  Processing by UsersController#index as HTML
  [1m [35mUser Load (0.2ms) [0m  SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
  Redirected to http://test.host/signin

Does this mean that every test index action is followed by a redirect to signin? If this were true it would seem consistent with tests including the test_sign_in method (above) failing and other tests like this one passing:

describe "GET 'index' for non-signed-in users" do     
  it "should deny access" do
    get :index
    response.should redirect_to(signin_path)
  end
end

I'm going to try to wrap my head around the test_sign_in method again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2024-12-29 10:52:36

您需要在 sign_in 帮助器中调用 self

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end

You need to call self in the sign_in helper.

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文