gpt4 book ai didi

ruby-on-rails - Michael Hartl 的 Ruby on Rails 教程第 8 章测试失败

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:05 37 4
gpt4 key购买 nike

我是 Rails 新手。我正在学习 Michael Hartl 的教程,Ruby on Rails 教程 - 使用 Rails 学习 Web 开发 (http://ruby.railstutorial.org/)。我在尝试通过一些测试时发现错误。我执行 bundle exec rspec 时的输出是下一个:

.........................................F.....

Failures:

1) Authentication signin with invalid information
Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
expected css "div.alert.alert-error" with text "Invalid" to return something
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'

Finished in 1.39 seconds
47 examples, 1 failure

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:23 # Authentication signin with invalid information

下一个文件的改动更大,我认为其中一个文件可能导致错误:

authentication_pages_rspec.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

session 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_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end

def destroy
end
end

sessions_helper.rb

module SessionsHelper

def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user;
end

def signed_in?
!current_user.nil?
end

def current_user=(user)
@current_user = user
end

def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
end

如果您需要任何其他文件,请告诉我,我会发布。提前谢谢你。

最佳答案

我已经浏览过你的代码,除了

中的分号
 self.current_user = user;

我没有看到任何奇怪的东西。您还没有发布您正在测试的方法,

if user && user.authenticate(params[:session][:password])

虽然,所以问题可能就在那里。

查找问题的一般步骤:

  • 如果您正在使用它,请重新启动它。
  • 手动测试失败的规范:功能是否有效?
  • 如果没有,修复它
  • 如果功能应该有效,但规范仍然失败,请在测试期间使用 launchy 和 save_and_open_page 查看页面。

补充 1:

好吧,那部分在你的规范中

before { visit signin_path }

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

引导您到 SessionController 创建操作(因为链接链接到映射到 config/routes.rb 中的 signin_path)并且可能出错的主要逻辑是 user.authenticate(params[:session][ :password]) - 由 has_secure_password 提供。

关于ruby-on-rails - Michael Hartl 的 Ruby on Rails 教程第 8 章测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702654/

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