【问题标题】:Rails Tutorial 9.3 failed test updating User authenticationRails Tutorial 9.3 测试更新用户身份验证失败
【发布时间】:2013-11-18 21:14:29
【问题描述】:

我在使用 Rails 教程(rails 3)时遇到问题。我目前正在进行 9.28 rSpec 测试,我一直在不断寻找我做了什么来犯这两个错误。

 user@ubuntu:~/rails_projects/sample_app$ bundle exec rspec spec/
 ......................F.F............................................

 Failures:

 1) Authentication authorization in the Users controller visiting the edit page 
 Failure/Error: before { visit edit_user_path(user) }
 NameError:
   undefined local variable or method `user' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3::Nested_2::Nested_1:0x9685354>
 # ./spec/requests/authentication_pages_spec.rb:75:in `block (5 levels) in <top (required)>'

 2) Authentication authorization in the Users controller submitting to the update action 
 Failure/Error: before { put user_path(user) }
 NameError:
   undefined local variable or method `user' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_3::Nested_2::Nested_3:0x9b12034>
 # ./spec/requests/authentication_pages_spec.rb:85:in `block (5 levels) in <top (required)>'

 Finished in 10.86 seconds
 69 examples, 2 failures

 Failed examples:

 rspec ./spec/requests/authentication_pages_spec.rb:76 # Authentication authorization in the Users controller visiting the edit page 
 rspec ./spec/requests/authentication_pages_spec.rb:86 # Authentication authorization in the Users controller submitting to the update action 

这是身份验证规范:

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: full_title('Sign in')) }
  end

  describe "signin" do
    before { visit signin_path }

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

      it { should have_selector('title', text: full_title('Sign in')) }
      it { should have_selector('div.alert.alert-error', text: 'Invalid')}

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



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


      it { should have_selector('title',       text:    full_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',    href: signin_path) }
      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_selector('title', text: ('Edit user'))

          end
        end
      end
    end

    describe "in the Users controller" do

      describe "visiting the edit page" do
        before { visit edit_user_path(user) }
        it { should have_selector('title', text: full_title('Sign in')) }
      end

      describe "visiting the user index" do
        before { visit users_path }
        it { should have_selector('title', text: full_title('Sign in')) }
      end

      describe "submitting to the update action" do
        before { put user_path(user) }
        specify { response.should 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 PUT request to the Users#update action" do
        before { put user_path(wrong_user) }
        specify { expect(response).to redirect_to(root_url) }
      end
    end
  end
end

真的,我已经尝试解决这个问题很长时间了,如果可以通过解释(如果不是错字的话)会很棒。开始掌握 Rails,但还没有完全掌握。

【问题讨论】:

  • 如果你的缩进正确,它会更清晰(对你和那些想要帮助你的人来说)。每个缩进使用两个空格,并使用 StackOverflow 中的 {} 按钮对整个代码块进行代码引用。
  • 我尝试修复缩进的授权代码,谢谢您的建议。

标签: ruby-on-rails ruby ruby-on-rails-3 rspec railstutorial.org


【解决方案1】:

如果您重新缩进凌乱的规范代码,您会发现您在 describe "in the Users controller" 块中缺少用户定义。

看到您在 Authorization 的所有三个 describe 块中都使用了 user,我会将其上移一级,因此您最终会得到:

describe "authorization" do
  let(:user) { FactoryGirl.create(:user) }

  describe "for non-signed-in users" do
    ...
  end

  describe "in the Users controller" do
    ...
  end

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

    ...
  end
end

【讨论】:

  • 谢谢机械鱼。在在线以及在 stackoverflow 上清理我的代码之后,我有一个结束 for_non-signed-in_users 块的结尾,因此之后没有在代码中声明用户。我也做了你的修复,将用户移动到一个类/巢/层(这就是它的名字吗?)它的工作原理是一样的。
  • 非常感谢。如果有人能给他答案的认可,那就太好了。
  • 人们称它为描述块,不知道它是否有更好的名字,因为我不经常使用rspec。你也应该能够接受答案。
猜你喜欢
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多