Rails 教程第 9.1.1 章 sign_in 无法识别 [英] Rails Tutorial chapter 9.1.1 sign_in unrecognised

查看:51
本文介绍了Rails 教程第 9.1.1 章 sign_in 无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Michael Hartl 的教程.

I'm working through Michael Hartl's tutorial.

我刚刚向 user_pages_spec.rb

describe "edit" do
  let(:user) { FactoryGirl.create(:user) }
  before do
    sign_in user
    visit edit_user_path(user)
  end

  describe "page" do
    it { should have_content("Update your profile") }
    it { should have_title("Edit user") }
    it { should have_link('change', href: 'http://gravatar.com/emails') }
  end

  describe "with invalid information" do
    before { click_button "Save changes" }

    it { should have_content('error') }
  end
end

我的 sessions_helper.rb 文件是:

module SessionsHelper
  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user= user
  end

  def current_user=(user)
    @current_user=user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def signed_in?
    !current_user.nil?
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end
end

当我运行测试时,所有 4 个测试都失败并显示以下内容的错误消息:

When I run the tests, all 4 fail with error messages along the lines of:

1) User pages edit page 
   Failure/Error: sign_in user
   NoMethodError:
     undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_1:0x007fcd57928138>
   # ./spec/requests/user_pages_spec.rb:90:in `block (3 levels) in <top (required)>'

我应该如何让用户页面规范从 session_helper 获取 sign_in 功能?users_controller 可以使用它;只是测试文件我做错了.

How should I get the user pages specs to pick up the sign_in function from sessions_helper? users_controller can use it; it's just the test files that I've done wrong.

我尝试明确包含 SessionsHelper,但没有帮助.

I tried including SessionsHelper explicitly and it didn't help.

推荐答案

列表 9.1最近的更改 这与直到代码清单 9.6 才引入此方法供 RSpec 使用的事实不一致.结果证明原始版本是正确的,并且对 sign_in user 的调用已被删除.

The introduction of the sign_in user call in Listing 9.1 was a recent change that was inconsistent with the fact that this method isn't introduced for use by RSpec until Listing 9.6. It turns out the original version was correct and that call to sign_in user has since been removed.

这篇关于Rails 教程第 9.1.1 章 sign_in 无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆