我目前正在迈克尔哈特尔教程的第7章,我一直在试用失败的测试套件。
当我运行bundle exec rspec spec/requests/user_pages_spec.rb
时,我遇到以下故障。任何人都可以对此有所了解吗?
Failures:
1) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:in `current_user'
# ./app/helpers/sessions_helper.rb:11:in `signed_in?'
# ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__873134851071575186_70192169771720'
# ./app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
2) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:in `current_user'
# ./app/helpers/sessions_helper.rb:11:in `signed_in?'
# ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__873134851071575186_70192169771720'
# ./app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
3) User pages signup page
Failure/Error: before { visit signup_path }
ActionView::Template::Error:
First argument in form cannot contain nil or be empty
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2899461793876151964_70192172593300'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
4) User pages signup page
Failure/Error: before { visit signup_path }
ActionView::Template::Error:
First argument in form cannot contain nil or be empty
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2899461793876151964_70192172593300'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
以下是我的user_spec.rb:
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
expect(@user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
addresses.each do |valid_address|
@user.email = valid_address
expect(@user).to be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { @user.password = @user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { @user.save }
let(:found_user) { User.find_by(email: @user.email) }
describe "with valid password" do
it { should eq found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
end
这是我的user_pages_spec.rb:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
以下是users_helper.rb:
module UsersHelper
# Returns the Gravatar (http://gravatar.com/) for the given user.
def gravatar_for(user)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
有人可以对此有所了解吗?
答案 0 :(得分:1)
虽然您提到您目前正在阅读本书的第7章,但正在发生的测试失败会出现在Chapter 8中。也许您应该检查代码库的状态是否与您当前在书中的位置相匹配。
例如,User.digest
中对app/helpers/sessions_helper.rb
的引用。
失败:
1)用户页面个人资料页面
失败/错误:在{visit user_path(user)}之前 ::的ActionView ::模板错误:
未定义的方法digest' for #<Class:0x007fadc6123b28>
current_user'
# ./app/helpers/sessions_helper.rb:19:in
#。/ app / help / session_helper.rb:11:insigned_in?'
_ app_views_layouts__header_html_erb__873134851071575186_70192169771720'
# ./app/views/layouts/_header.html.erb:9:in
'./app/views/layouts/application.html.erb:12:in_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
阻止(3级)'
# ./spec/requests/user_pages_spec.rb:9:in