Rails教程第10.3.1节无法通过Rspec测试

时间:2014-01-17 23:54:16

标签: ruby-on-rails railstutorial.org

查看标题。这是我的失败,对于我的生活,我无法弄清楚为什么在10.3.2之前失败了。在10.3.1之前一切正常,并测试网站本身,一切似乎都没问题。

Failures:

  1) Authentication authorization in the Microposts controller submitting to the create action 
     Failure/Error: before { post microposts_path }
     NoMethodError:
       undefined method `sign_in?' for #<MicropostsController:0x007faa8eb32898>
     # ./app/helpers/sessions_helper.rb:28:in `signed_in_user'
     # ./spec/requests/authentication_pages_spec.rb:106:in `block (5 levels) in <top (required)>'

  2) Authentication authorization in the Microposts controller submitting to the destroy action 
     Failure/Error: before { delete micropost_path(FactoryGirl.create(:micropost)) }
     NoMethodError:
       undefined method `sign_in?' for #<MicropostsController:0x007faa8dc18a88>
     # ./app/helpers/sessions_helper.rb:28:in `signed_in_user'
     # ./spec/requests/authentication_pages_spec.rb:111:in `block (5 levels) in <top (required)>'

Finished in 3.31 seconds
22 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:107 # Authentication authorization in the Microposts controller submitting to the create action 
rspec ./spec/requests/authentication_pages_spec.rb:112 # Authentication authorization in the Microposts controller submitting to the destroy action 

这是我的spec / requests / authentication_pages_spec.rb文件:

require 'spec_helper'

describe "Authentication" do

subject { page }

# verify that non-signed-in users attempting to access either action 
# are simply sent to the signin page

describe "signin page" do
before { visit signin_path }

it { should have_content('Sign in') }
it { should have_title('Sign in') }
end

describe "signin" do
before { visit signin_path }

describe "with invalid information" do
  before { click_button "Sign in" }

  it { should have_title('Sign in') }
  it { should have_selector('div.alert.alert-error') }

  describe "after visiting another page" do
    before { click_link "Home" }
    it { should_not have_selector('div.alert.alert-error') }
  end

end

describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      # before { sign_in user }

  before do
    fill_in "Email",    with: user.email.upcase
    fill_in "Password", with: user.password
    click_button "Sign in"
  end

  it { should have_title(user.name) }
  it { should have_link('Users',       href: users_path) }
      it { should have_link('Profile',     href: user_path(user)) }
      it { should have_link('Settings',    href: edit_user_path(user)) }
      it { should have_link('Sign out',    href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }


      describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
    #it { should_not have_link('Settings') }
   end
 end
end

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

    describe "when attempting to visit a protected page" do
        before do
            visit edit_user_path(user)
            fill_in "Email",    with: user.email
            fill_in "Password", with: user.password
            click_button "Sign in"
        end
    describe "after signing in" do

            it "should render the desired protected page" do
                expect(page).to have_title('Edit user')
            end
        end

    end

    describe "in the Users controller" do

        describe "visiting the edit page" do
            before { visit edit_user_path(user) }
            it { should have_title('Sign in') }
        end

        describe "submitting to the update action" do
            before { patch user_path(user) }
            specify { expect(response).to redirect_to(signin_path) }
                # tests response of the server
            end

            describe "visiting the user index" do
            before { visit users_path }
            it { should have_title('Sign in') }
        end
        end
    end

describe "in the Microposts controller" do

  describe "submitting to the create action" do
    before { post microposts_path }
    specify { expect(response).to redirect_to(signin_path) }
  end

  describe "submitting to the destroy action" do
    before { delete micropost_path(FactoryGirl.create(:micropost)) }
    specify { expect(response).to redirect_to(signin_path) }
  end
end

describe "as wrong user" do
  let(:user) { FactoryGirl.create(:user) }
  let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
  before { sign_in user, no_capybara: true }

  describe "submitting a GET request to the Users#edit action" do
    before { get edit_user_path(wrong_user) }
    specify { expect(response.body).not_to match(full_title('Edit user')) }
    specify { expect(response).to redirect_to(root_url) }
  end

  describe "submitting a PATCH request to the User#update action" do
    before { patch user_path(wrong_user) }
    specify { expect(response).to redirect_to(root_url) }
  end
end

describe "as non-admin user" do
  let(:user) { FactoryGirl.create(:user) }
  let(:non_admin) { FactoryGirl.create(:user) }

  before { sign_in non_admin, no_capybara: true }

  describe "submitting a DELETE request to the Users#destroy action" do
    before { delete user_path(user) }
    specify { expect(response).to redirect_to(root_url) }
  end
end
end




end

1 个答案:

答案 0 :(得分:0)

检查/app/helpers/sessions_helper.rb

该方法必须为signed_in?而不是sign_in?

  def signed_in_user
    unless signed_in?
      store_location
      redirect_to signin_url, notice: "Please sign in."
    end
  end

http://ruby.railstutorial.org/chapters/user-microposts#code-sessions_helper_authenticate