1

我正在阅读 Michael Hartl 的教程。

我刚刚介绍了这些测试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 个都失败,错误消息如下:

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 可以使用;这只是我做错的测试文件。

我尝试明确包含 SessionsHelper 并没有帮助。

4

1 回答 1

1

清单 9.1sign_in user中调用的引入是最近的更改,这与该方法直到清单 9.6 才引入 RSpec 使用这一事实不一致。事实证明原始版本是正确的,并且该调用已被删除。sign_in user

于 2013-09-17T22:35:13.187 回答