【问题标题】:Michael Hartl's Ruby on Rails Tutorial: Chapter 9, Exercise 3Michael Hartl 的 Ruby on Rails 教程:第 9 章,练习 3
【发布时间】:2013-09-06 23:30:28
【问题描述】:

这里显然是个新手,但正在阅读 Hartl 的书。我似乎不知道如何为练习 3 编写正确的测试:“当前的身份验证测试会检查用户登录时是否出现“个人资料”和“设置”等导航链接。添加测试以确保当用户未登录时,这些链接不会出现。”:

这是我的 authentication_pages_spec.rb:

require 'spec_helper'

describe "AuthenticationPages" do

subject { page }

  describe "signin path" do
    before { visit signin_path }

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

  describe "should not have show profile and settings in menu" do

    it { should_not have_link('Profile',        href: user_path(user)) }
    it { should_not have_link('Settings',   href: edit_user_path(user)) }
  end

  describe "signin" do
    before { visit signin_path }

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

      it { should have_selector('title', text: '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 "with valid signin information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user } 

      it { should have_selector('title', text: 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') }
      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
              page.should have_selector('title', text: '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_selector('title', text: 'Sign in') }
          end

          describe "submitting to the update action" do
            before { put user_path(user) }
            specify { response.should redirect_to(signin_path) }
          end

          describe "visiting the user index" do
            before { visit users_path }
            it { should have_selector('title', text: "Sign in") }
          end
        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 }

        describe "visiting Users#edit page" do
          before { visit edit_user_path(wrong_user) }
          it { should_not have_selector('title', text: full_title('Edit user')) }
        end

        describe "submitting a PUT request to the Users#update action" do
          before { put user_path(wrong_user) }
          specify { response.should redirect_to(root_url) }
        end
      end

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

        before { sign_in non_admin }

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

我添加的行的测试仍然失败。任何见解都会有所帮助。谢谢。

【问题讨论】:

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


    【解决方案1】:

    我可能遗漏了一些东西,但我看不到您为此练习添加了哪些测试。

    我刚刚完成了这个,现在第一个 AuthenticationPages 测试看起来像这样。

    authentication_pages_spec.rb

    describe "sigin page" do
      before { visit signin_path }
    
      it { should have_content('Sign in') }
      it { should have_title('Sign in') }
    
      it { should_not have_link('Users') }
      it { should_not have_link('Profile') }
      it { should_not have_link('Settings') }
      it { should_not have_link('Sign out', href: signout_path) }
      it { should have_link('Sign in', href: signin_path) }
    end
    

    【讨论】:

    • 我认为我的某个测试部分下的行数过多。
    【解决方案2】:

    除了上述 KnownColor 的测试之外,我还添加了以下测试,以验证在用户未登录时链接不会显示在个人资料页面上:

    authentication_pages_spec.rb

    describe "authorization" do
    
      describe "for non-signed-in users" do
    
        let(:user) {FactoryGirl.create(:user)}
    
        describe "when visiting a non-protected page" do
          before {visit user_path(user)}
          it {should have_link('Sign in', href: signin_path)}
          it {should_not have_link('Users')}
          it {should_not have_link('Profile')}
          it {should_not have_link('Settings')}
          it {should_not have_link('Sign out')}
        end
        ...
      end
      ...
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多