gpt4 book ai didi

ruby-on-rails - Rails 教程 : Chapter 8. 1.5 错误(使用闪现消息呈现; list 8.11 和 8.12)

转载 作者:行者123 更新时间:2023-11-28 21:13:46 25 4
gpt4 key购买 nike

我正在学习 RoR 教程。在第 8.1.5 章的最后一部分,当所有的 rspec 测试都应该变成绿色时,我就是无法让我的测试工作。我运行 bundle exec rspec spec/requests/authentication_pages_spec.rb -e "signin with invalid information"并且在四个测试中,一个失败,出现以下错误:

1) Authentication signin with invalid information after visiting another page 
Failure/Error: it { should_not have_selector('div.alert.alert-error') }
expected css "div.alert.alert-error" not to return anything
# ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'

app/controllers/sessions_controller.rb 的文本如下:

class SessionsController < ApplicationController

def new
end

def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end

def destroy
end
end

spec/requests/authetication_pages_spec.rb 的文本如下:

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 "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end

describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end

it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
end
end

如果您有任何想法或想法,请告诉我,我完全被难住了!

最佳答案

我克隆了 your repo并找到了您的问题。

头文件 (app/views/layouts/_header.html.erb) 中的链接不会去任何地方:

<li><%= link_to "Home",    '#' %></li>
<li><%= link_to "Help", '#' %></li>

因此,当您的测试运行时,以下测试通过,因为显示错误...

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 "Home" } # goes nowhere
it { should_not have_selector('div.alert.alert-error') }
end
end

...该错误仍在页面上,因此测试正确地失败了。

一旦您将 “Home” 链接目标从 '#' 更改为 root_path,您的测试应该会通过。

看看正文,如果你在看第 8 章,你似乎错过了前几章 Listing 5.2.4 的一步在 Chapter 5.3.3 ,所以一旦您返回并修复它,您应该处于更好的状态。

我还建议安装 launchy gem,这样您就可以在测试中调用有用的 Capybara 方法,例如 save_and_open_page,它会为您打开浏览器并显示状态在您调用该方法时您的页面;对调试有很大帮助。

关于ruby-on-rails - Rails 教程 : Chapter 8. 1.5 错误(使用闪现消息呈现; list 8.11 和 8.12),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11445238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com