Ruby on rails hartl第9.2章-应用程序功能良好的rspec/工厂问题,但测试失败

Ruby on rails hartl第9.2章-应用程序功能良好的rspec/工厂问题,但测试失败,ruby-on-rails,factory-bot,rspec-rails,railstutorial.org,Ruby On Rails,Factory Bot,Rspec Rails,Railstutorial.org,在我最后一次修改Hartl教程中的rspec测试之前,所有测试都进行得很好。此时,我的用户页面和应用程序页面中的所有相关测试都失败了 虽然我检查了网站,但失败并没有出现在现实生活中——因此我认为测试套件试图模拟用户的方式存在问题——这是有道理的,因为我改变了这一点 有一件事需要注意——我已经对Hartl教程进行了一些调整,但大多数都是修饰性的或次要的——这里唯一重要的是我使用用户名而不是电子邮件来登录和查找 下面是完整的文件-非常感谢您的帮助,因为我已经用头撞墙好几个小时了,这让我很沮丧 spe

在我最后一次修改Hartl教程中的rspec测试之前,所有测试都进行得很好。此时,我的用户页面和应用程序页面中的所有相关测试都失败了

虽然我检查了网站,但失败并没有出现在现实生活中——因此我认为测试套件试图模拟用户的方式存在问题——这是有道理的,因为我改变了这一点

有一件事需要注意——我已经对Hartl教程进行了一些调整,但大多数都是修饰性的或次要的——这里唯一重要的是我使用用户名而不是电子邮件来登录和查找

下面是完整的文件-非常感谢您的帮助,因为我已经用头撞墙好几个小时了,这让我很沮丧

spec/support/utilities.rb:

def full_title(page_title)
  base_title = "nsent"
  if page_title.empty?
    base_title
  else
    "#{base_title} | #{page_title}"
  end
end

def sign_in(user)
  visit signin_path
  fill_in "Name",    with: user.name
  fill_in "Password", with: user.password
  click_button "Sign in"
  # Sign in when not using Capybara as well.
  cookies[:remember_token] = user.remember_token
end
spec/support/factories.rb:

FactoryGirl.define do
  factory :user do
    name     "Example User"
    email    "user@example.com"
    password "foobar"
    password_confirmation "foobar"
  end
end
用户\u页面\u spec.rb:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "signup" do

    before { visit signup_path }

    describe "should have the right title and heading" do
            it { should have_selector('h1',    text: "Sign up") }
        it { should have_selector('title', text: full_title("Sign up")) }
    end

    let(:submit) { "Sign up" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end
      describe "after submission" do
        before {click_button submit }

        it { should have_selector('title', text: "Sign up") }
        it { should have_content('field') }
      end
    end

    describe "with valid information" do
      before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Password confirmation", with: "foobar"
      end

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
      end

      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by_email('user@example.com') }

        it { should have_selector('title', text: user.name) }
        it { should have_selector('div.alert.alert-success', text: 'welcome') }
        it { should have_link('Sign out') }
      end
        end
  end

    describe "profile page" do
      let(:user) { FactoryGirl.create(:user) }
      before { visit user_path(user) }

      it { should have_selector('h1',    text: user.name) }
      it { should have_selector('title', text: user.name) }
    end

  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_selector('h1',    text: "Update your profile") }
      it { should have_selector('title', text: "Edit user") }
      it { should have_link('Change Profile Picture', href: 'http://gravatar.com/emails') }
    end

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

      it { should have_content('Whoops') }
    end

    describe "with valid information" do
      let(:new_name)  { "New Name" }
      let(:new_email) { "new@example.com" }
      before do
        fill_in "Name",             with: new_name
        fill_in "Password",         with: user.password
        fill_in "Password confirmation", with: user.password
        fill_in "Email",            with: new_email
        click_button "Save changes"
      end

      it { should have_selector('title', text: new_name) }
      it { should have_selector('div.alert.alert-success') }
      it { should have_link('Sign out', href: signout_path) }
      specify { user.reload.name.should  == new_name }
      specify { user.reload.email.should == new_email }
    end
  end
end
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: '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: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      describe "after visiting another page" do
        before { click_link "nsent"}
        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 }

      it { should have_selector('title', text: user.name) }
      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) }
        it { should_not have_link('Profile',  href: user_path(user)) }
        it { should_not have_link('Settings', href: edit_user_path(user)) }
        it { should_not have_link('Sign out', href: signout_path) }
      end
    end
  end

    describe "authorization" do

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

      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
      end
    end
  end
end
用户\u页面\u spec.rb故障(从终端输出):

应用程序\u页面\u规范rb故障(终端输出):

故障:
1) 使用有效信息进行身份验证登录
失败/错误:它{应该有_选择器('title',text:user.name)}
预期css“title”和文本“Example User”返回一些内容
#./spec/requests/authentication\u pages\u spec.rb:33:in'block(4级)in'
2) 使用有效信息进行身份验证登录
失败/错误:它{应该有_链接('Settings',href:edit_user_path(user))}
期望链接“设置”返回某些内容
#./spec/requests/authentication\u pages\u spec.rb:35:in'block(4级)in'
3) 使用有效信息进行身份验证登录
失败/错误:它{应该有_链接('Profile',href:user_path(user))}
期望链接“Profile”返回某些内容
#./spec/requests/authentication\u pages\u spec.rb:34:in'block(4级)in'
4) 使用有效信息进行身份验证登录
失败/错误:它{应该没有链接('Sign-in',href:signin\u path)}
预期链接“登录”不会返回任何内容
#./spec/requests/authentication\u pages\u spec.rb:37:in'block(4级)in'
5) 使用有效信息进行身份验证登录
失败/错误:它{应该有_链接('signout',href:signout_path)}
期望链接“注销”以返回某些内容
#./spec/requests/authentication\u pages\u spec.rb:36:in'block(4级)in'
6) 使用有效信息进行身份验证登录,然后注销
失败/错误:在{单击链接“注销”}之前
Capybara::ElementNotFound:
找不到标题、id或文本为“注销”的链接
#(评估):2:在“点击链接”中
#./spec/requests/authentication\u pages\u spec.rb:40:in'block(5级)in'
7) 使用有效信息进行身份验证登录,然后注销
失败/错误:在{单击链接“注销”}之前
Capybara::ElementNotFound:
找不到标题、id或文本为“注销”的链接
#(评估):2:在“点击链接”中
#./spec/requests/authentication\u pages\u spec.rb:40:in'block(5级)in'
8) 使用有效信息进行身份验证登录,然后注销
失败/错误:在{单击链接“注销”}之前
Capybara::ElementNotFound:
找不到标题、id或文本为“注销”的链接
#(评估):2:在“点击链接”中
#./spec/requests/authentication\u pages\u spec.rb:40:in'block(5级)in'
9) 使用有效信息进行身份验证登录,然后注销
失败/错误:在{单击链接“注销”}之前
Capybara::ElementNotFound:
找不到标题、id或文本为“注销”的链接
#(评估):2:在“点击链接”中
#./spec/requests/authentication\u pages\u spec.rb:40:in'block(5级)in'
10) 使用无效信息编辑用户页面
失败/错误:{单击按钮“保存更改”之前}
Capybara::ElementNotFound:
找不到具有值、id或文本“保存更改”的按钮
#(评估):2:在“单击按钮”中
#./spec/requests/user\u pages\u spec.rb:75:in'block(4级)in'
11) 使用有效信息编辑用户页面
失败/错误:在“密码确认”中填写:user.Password
Capybara::ElementNotFound:
无法填写,找不到id、名称或标签为“密码确认”的文本字段、文本区域或密码字段
#(评估):2:填写“填写”
#./spec/requests/user\u pages\u spec.rb:86:in'block(4级)in'
12) 使用有效信息编辑用户页面
失败/错误:在“密码确认”中填写:user.Password
Capybara::ElementNotFound:
无法填写,找不到id、名称或标签为“密码确认”的文本字段、文本区域或密码字段
#(评估):2:填写“填写”
#./spec/requests/user\u pages\u spec.rb:86:in'block(4级)in'
13) 使用有效信息编辑用户页面
失败/错误:在“密码确认”中填写:user.Password
Capybara::ElementNotFound:
无法填写,找不到id、名称或标签为“密码确认”的文本字段、文本区域或密码字段
#(评估):2:填写“填写”
#./spec/requests/user\u pages\u spec.rb:86:in'block(4级)in'
14) 使用有效信息编辑用户页面
失败/错误:在“密码确认”中填写:user.Password
Capybara::ElementNotFound:
无法填写,找不到id、名称或标签为“密码确认”的文本字段、文本区域或密码字段
#(评估):2:填写“填写”
#./spec/requests/user\u pages\u spec.rb:86:in'block(4级)in'
15) 使用有效信息编辑用户页面
失败/错误:在“密码确认”中填写:user.Password
Capybara::ElementNotFound:
无法填写,找不到id、名称或标签为“密码确认”的文本字段、文本区域或密码字段
#(评估):2:填写“填写”
#./spec/requests/user\u pages\u spec.rb:86:in'block(4级)in'
16) 用户页面编辑页面
失败/错误:它{应该有_选择器('title',text
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: '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: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      describe "after visiting another page" do
        before { click_link "nsent"}
        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 }

      it { should have_selector('title', text: user.name) }
      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) }
        it { should_not have_link('Profile',  href: user_path(user)) }
        it { should_not have_link('Settings', href: edit_user_path(user)) }
        it { should_not have_link('Sign out', href: signout_path) }
      end
    end
  end

    describe "authorization" do

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

      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
      end
    end
  end
end
Failures:

  1) Authentication signin with valid information
     Failure/Error: it { should have_selector('title', text: user.name) }
       expected css "title" with text "Example User" to return something
     # ./spec/requests/authentication_pages_spec.rb:33:in `block (4 levels) in <top (required)>'

  2) Authentication signin with valid information
     Failure/Error: it { should have_link('Settings', href: edit_user_path(user)) }
       expected link "Settings" to return something
     # ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

  3) Authentication signin with valid information
     Failure/Error: it { should have_link('Profile',  href: user_path(user)) }
       expected link "Profile" to return something
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

  4) Authentication signin with valid information
     Failure/Error: it { should_not have_link('Sign in', href: signin_path) }
       expected link "Sign in" not to return anything
     # ./spec/requests/authentication_pages_spec.rb:37:in `block (4 levels) in <top (required)>'

  5) Authentication signin with valid information
     Failure/Error: it { should have_link('Sign out', href: signout_path) }
       expected link "Sign out" to return something
     # ./spec/requests/authentication_pages_spec.rb:36:in `block (4 levels) in <top (required)>'

  6) Authentication signin with valid information followed by signout
     Failure/Error: before { click_link "Sign out" }
     Capybara::ElementNotFound:
       no link with title, id or text 'Sign out' found
     # (eval):2:in `click_link'
     # ./spec/requests/authentication_pages_spec.rb:40:in `block (5 levels) in <top (required)>'

  7) Authentication signin with valid information followed by signout
     Failure/Error: before { click_link "Sign out" }
     Capybara::ElementNotFound:
       no link with title, id or text 'Sign out' found
     # (eval):2:in `click_link'
     # ./spec/requests/authentication_pages_spec.rb:40:in `block (5 levels) in <top (required)>'

  8) Authentication signin with valid information followed by signout
     Failure/Error: before { click_link "Sign out" }
     Capybara::ElementNotFound:
       no link with title, id or text 'Sign out' found
     # (eval):2:in `click_link'
     # ./spec/requests/authentication_pages_spec.rb:40:in `block (5 levels) in <top (required)>'

  9) Authentication signin with valid information followed by signout
     Failure/Error: before { click_link "Sign out" }
     Capybara::ElementNotFound:
       no link with title, id or text 'Sign out' found
     # (eval):2:in `click_link'
     # ./spec/requests/authentication_pages_spec.rb:40:in `block (5 levels) in <top (required)>'

  10) User pages edit with invalid information
     Failure/Error: before { click_button "Save changes" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Save changes' found
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:75:in `block (4 levels) in <top (required)>'

  11) User pages edit with valid information
     Failure/Error: fill_in "Password confirmation", with: user.password
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'Password confirmation' found
     # (eval):2:in `fill_in'
     # ./spec/requests/user_pages_spec.rb:86:in `block (4 levels) in <top (required)>'

  12) User pages edit with valid information
     Failure/Error: fill_in "Password confirmation", with: user.password
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'Password confirmation' found
     # (eval):2:in `fill_in'
     # ./spec/requests/user_pages_spec.rb:86:in `block (4 levels) in <top (required)>'

  13) User pages edit with valid information
     Failure/Error: fill_in "Password confirmation", with: user.password
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'Password confirmation' found
     # (eval):2:in `fill_in'
     # ./spec/requests/user_pages_spec.rb:86:in `block (4 levels) in <top (required)>'

  14) User pages edit with valid information
     Failure/Error: fill_in "Password confirmation", with: user.password
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'Password confirmation' found
     # (eval):2:in `fill_in'
     # ./spec/requests/user_pages_spec.rb:86:in `block (4 levels) in <top (required)>'

  15) User pages edit with valid information
     Failure/Error: fill_in "Password confirmation", with: user.password
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'Password confirmation' found
     # (eval):2:in `fill_in'
     # ./spec/requests/user_pages_spec.rb:86:in `block (4 levels) in <top (required)>'

  16) User pages edit page
     Failure/Error: it { should have_selector('title', text: "Edit user") }
       expected css "title" with text "Edit user" to return something
     # ./spec/requests/user_pages_spec.rb:70:in `block (4 levels) in <top (required)>'

  17) User pages edit page
     Failure/Error: it { should have_link('Change Profile Picture', href: 'http://gravatar.com/emails') }
       expected link "Change Profile Picture" to return something
     # ./spec/requests/user_pages_spec.rb:71:in `block (4 levels) in <top (required)>'

  18) User pages edit page
     Failure/Error: it { should have_selector('h1',    text: "Update your profile") }
       expected css "h1" with text "Update your profile" to return something
     # ./spec/requests/user_pages_spec.rb:69:in `block (4 levels) in <top (required)>'

Finished in 1.53 seconds
36 examples, 18 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:33 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:35 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:34 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:37 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:36 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:42 # Authentication signin with valid information followed by signout
rspec ./spec/requests/authentication_pages_spec.rb:44 # Authentication signin with valid information followed by signout
rspec ./spec/requests/authentication_pages_spec.rb:41 # Authentication signin with valid information followed by signout
rspec ./spec/requests/authentication_pages_spec.rb:43 # Authentication signin with valid information followed by signout
rspec ./spec/requests/user_pages_spec.rb:77 # User pages edit with invalid information
rspec ./spec/requests/user_pages_spec.rb:91 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:93 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:92 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:95 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:94 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:70 # User pages edit page
rspec ./spec/requests/user_pages_spec.rb:71 # User pages edit page
rspec ./spec/requests/user_pages_spec.rb:69 # User pages edit page
 describe "with invalid information" do
      before do
      visit edit_user_path(user)
      click_button "Save changes"
      end

      it { should have_content('Whoops') }
 end