Ruby on rails Michael Hartl的Rails教程第7.2.2章注册-失败测试
我检查了所有页面的代码,它们看起来与Hartl的教程完全相同。尽管如此,当我运行bundle exec rspec spec/时,我还是遇到了这些失败的测试错误,我非常困惑:Ruby on rails Michael Hartl的Rails教程第7.2.2章注册-失败测试,ruby-on-rails,ruby,ruby-on-rails-3,rspec,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,我检查了所有页面的代码,它们看起来与Hartl的教程完全相同。尽管如此,当我运行bundle exec rspec spec/时,我还是遇到了这些失败的测试错误,我非常困惑: Pending: StaticPagesHelper add some examples to (or delete) /Users/thaobach/Desktop/sample_app/spec/helpers/static_pages_helper_spec.rb # No rea
Pending:
StaticPagesHelper add some examples to (or delete) /Users/thaobach/Desktop/sample_app/spec/helpers/static_pages_helper_spec.rb
# No reason given
# ./spec/helpers/static_pages_helper_spec.rb:14
Failures:
1) Static pages Contact page it should behave like all static pages
Failure/Error: it { should have_title('h1', text:heading) }
ArgumentError:
wrong number of arguments (2 for 1)
Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:62
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
2) Static pages About page it should behave like all static pages
Failure/Error: it { should have_title('h1', text:heading) }
ArgumentError:
wrong number of arguments (2 for 1)
Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:53
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
3) Static pages Home page it should behave like all static pages
Failure/Error: it { should have_title('h1', text:heading) }
ArgumentError:
wrong number of arguments (2 for 1)
Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:34
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
4) Static pages Help page it should behave like all static pages
Failure/Error: it { should have_title('h1', text:heading) }
ArgumentError:
wrong number of arguments (2 for 1)
Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:44
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
5) User pages profile page
Failure/Error: it { should have_title('title', text: user.name) }
ArgumentError:
wrong number of arguments (2 for 1)
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
6) User pages profile page
Failure/Error: it { should have_title('h1', text: user.name) }
ArgumentError:
wrong number of arguments (2 for 1)
# ./spec/requests/user_pages_spec.rb:11:in `block (3 levels) in <top (required)>'
7) User pages signup page
Failure/Error: it { should have_title('title', text: 'Sign Up') }
ArgumentError:
wrong number of arguments (2 for 1)
# ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>'
8) User pages signup page
Failure/Error: it { should have_title('h1', text: 'Sign Up') }
ArgumentError:
wrong number of arguments (2 for 1)
# ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'
config/routes.rb
SampleApp::Application.routes.draw do
resources :users
root to: 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
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_title('h1', text: user.name) }
it { should have_title('title', text: user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_title('h1', text: 'Sign Up') }
it { should have_title('title', text: 'Sign Up') }
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
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 "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
spec/requests/user\u pages\u spec.rb
SampleApp::Application.routes.draw do
resources :users
root to: 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
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_title('h1', text: user.name) }
it { should have_title('title', text: user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_title('h1', text: 'Sign Up') }
it { should have_title('title', text: 'Sign Up') }
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
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 "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
app/specs/requests/static\u pages\u spec.rb
require 'spec_helper'
describe StaticPagesController do
describe "GET '...'" do
it "returns http success" do
get '...'
response.should be_success
end
end
end
require 'spec_helper'
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_title('h1', text:heading) }
it { should have_title(full_title(page_title)) }
end
it "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
click_link "Help"
expect(page).to have_title(full_title('Help'))
click_link "Contact"
expect(page).to have_title(full_title('Contact'))
click_link "Home"
click_link "Sign up now!"
expect(page).to have_title(full_title('Sign up'))
click_link "sample app"
expect(page).to have_title(full_title(''))
end
describe "Home page" do
before { visit root_path }
let (:heading) { 'Sample App'}
let(:page_title) { '' }
it_should_behave_like "all static pages"
it { should_not have_title('| Home') }
end
describe "Help page" do
before { visit help_path }
let (:heading) { 'Help'}
let(:page_title) { 'Help' }
it_should_behave_like "all static pages"
end
describe "About page" do
before { visit about_path }
let (:heading) { 'About'}
let(:page_title) { 'About' }
it_should_behave_like "all static pages"
end
describe "Contact page" do
before { visit contact_path }
let (:heading) { 'Contact'}
let(:page_title) { 'Contact' }
it_should_behave_like "all static pages"
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
#Handle successful save
else
render 'new'
end
end
end
app/controllers/users\u controller.rb
require 'spec_helper'
describe StaticPagesController do
describe "GET '...'" do
it "returns http success" do
get '...'
response.should be_success
end
end
end
require 'spec_helper'
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_title('h1', text:heading) }
it { should have_title(full_title(page_title)) }
end
it "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
click_link "Help"
expect(page).to have_title(full_title('Help'))
click_link "Contact"
expect(page).to have_title(full_title('Contact'))
click_link "Home"
click_link "Sign up now!"
expect(page).to have_title(full_title('Sign up'))
click_link "sample app"
expect(page).to have_title(full_title(''))
end
describe "Home page" do
before { visit root_path }
let (:heading) { 'Sample App'}
let(:page_title) { '' }
it_should_behave_like "all static pages"
it { should_not have_title('| Home') }
end
describe "Help page" do
before { visit help_path }
let (:heading) { 'Help'}
let(:page_title) { 'Help' }
it_should_behave_like "all static pages"
end
describe "About page" do
before { visit about_path }
let (:heading) { 'About'}
let(:page_title) { 'About' }
it_should_behave_like "all static pages"
end
describe "Contact page" do
before { visit contact_path }
let (:heading) { 'Contact'}
let(:page_title) { 'Contact' }
it_should_behave_like "all static pages"
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
#Handle successful save
else
render 'new'
end
end
end
所有的错误几乎都是不言自明的,它会为你打破它
get'.''.
做什么,请再次参考教程resources:users,only:[:create]
4,5,6。在查看您的UsersController文件后,我可以告诉您更多信息。所有错误几乎都是不言自明的,它会为您打破它。您好,我已经添加了我的
users\u controller.rb
文件我还更新了错误列表。我意识到我的一些错误与使用Cabybara 2.1.0
有关,因此我修复了应该有\u选择器
到应该有\u标题
。现在,有一个新的错误列表。