0
我是Ruby on Rails的新手,我正在按照Michael Hartl的教程進行操作。我開始寫第8章,不知道我錯過了哪些步驟來獲得失敗。任何幫助是極大的讚賞。Ruby on Rails教程,第8章rspec錯誤
Failures:
1) User pages signup with invalid information should not create a user
Failure/Error: expect { click_button submit }.not_to change(User, :count)
ActionView::Template::Error:
undefined method `errors' for nil:NilClass
# ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
# ./app/controllers/users_controller.rb:23:in `create'
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:30:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:30:in `block (4 levels) in <top (required)>'
2) User pages signup with invalid information after submission
Failure/Error: before { click_button submit }
ActionView::Template::Error:
undefined method `errors' for nil:NilClass
# ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
# ./app/controllers/users_controller.rb:23:in `create'
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:33:in `block (5 levels) in <top (required)>'
3) User pages signup with invalid information after submission
Failure/Error: before { click_button submit }
ActionView::Template::Error:
undefined method `errors' for nil:NilClass
# ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
# ./app/controllers/users_controller.rb:23:in `create'
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:33:in `block (5 levels) in <top (required)>'
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@title = "Sign up"
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
end
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
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 "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
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('error') }
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "[email protected]"
fill_in "Password", with: "foobar"
fill_in "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('[email protected]') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
end
end
應用程序/視圖/共享/ _error_messages.html.erb
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error")%>
</div>
<ul>
<% @users.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
你可以發佈一個鏈接到教程(如果它在線)嗎?除非您提供鏈接或報價,否則我們不知道Michael Hartl教程的第8章是什麼。 – gparyani
Marek Lipka是正確的!我只專注於_error_messages.html.erb,不知道爲什麼它是零,並給我rspec失敗。我將用戶改爲用戶,現在一切都很好!再次感謝你。 – cat3045