我正在研究Michael Hartl的Ruby on Rails教程,我正在进行第5章练习。有人可以解释为什么这个测试失败了吗?
我的routes.rb
SampleApp::Application.routes.draw do
root 'static_pages#home'
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
我的static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the h1 'Sample App'" do
visit root_path
expect(page).to have_content('Sample App')
end
it "should have the base title" do
visit root_path
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit root_path
expect(page).not_to have_title('| Home')
end
end
describe "Help page" do
it "should have the h1 'Help'" do
visit help_path
expect(page).to have_content('Help')
end
it "should have the title 'Help'" do
visit help_path
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the h1 'About Us'" do
visit about_path
expect(page).to have_content('About Us')
end
it "should have the title 'About Us'" do
visit about_path
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit contact_path
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit contact_path
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
end
我的测试: gvyntyk @ gvyntyk-r60:〜/ rails_projects / sample_app $ rspec spec / requests / static_pages_spec.rb ....˚F... F
Failures:
1) Static pages Help page should have the title 'Help'
Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
expected #has_title?("Ruby on Rails Tutorial Sample App | Help") to return true, got false
# ./spec/requests/static_pages_spec.rb:34:in `block (3 levels) in <top (required)>'
2) Static pages About page should have the title 'About Us'
Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
expected #has_title?("Ruby on Rails Tutorial Sample App | About Us") to return true, got false
# ./spec/requests/static_pages_spec.rb:47:in `block (3 levels) in <top (required)>'
Finished in 0.67805 seconds
9 examples, 2 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:32 # Static pages Help page should have the title 'Help'
rspec ./spec/requests/static_pages_spec.rb:45 # Static pages About page should have the title 'About Us'
虽然网络应用程序有效,但我可以打开/关闭和/帮助页面。
gvyntyk@gvyntyk-r60:~/rails_projects/sample_app$ rails server
=> Booting WEBrick
=> Rails 4.0.2 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-01-25 20:01:19] INFO WEBrick 1.3.1
[2015-01-25 20:01:20] INFO ruby 2.0.0 (2014-11-13) [i686-linux]
[2015-01-25 20:01:20] INFO WEBrick::HTTPServer#start: pid=17314 port=3000
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2015-01-25 20:01:29 +0200
Started GET "/assets/rails.png" for 127.0.0.1 at 2015-01-25 20:01:31 +0200
Started GET "/help" for 127.0.0.1 at 2015-01-25 20:01:42 +0200
Processing by StaticPagesController#help as HTML
Rendered static_pages/help.html.erb within layouts/application (2.9ms)
Rendered layouts/_shim.html.erb (0.8ms)
Rendered layouts/_header.html.erb (1.8ms)
Rendered layouts/_footer.html.erb (1.4ms)
Completed 200 OK in 516ms (Views: 513.2ms | ActiveRecord: 0.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/assets/static_pages.css?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/assets/static_pages.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:43 +0200
Started GET "/about" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Processing by StaticPagesController#about as HTML
Rendered static_pages/about.html.erb within layouts/application (0.9ms)
Rendered layouts/_shim.html.erb (0.2ms)
Rendered layouts/_header.html.erb (2.9ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 56ms (Views: 55.2ms | ActiveRecord: 0.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Started GET "/assets/static_pages.css?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Started GET "/assets/static_pages.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2015-01-25 20:01:51 +0200
^C[2015-01-25 20:02:34] INFO going to shutdown ...
[2015-01-25 20:02:34] INFO WEBrick::HTTPServer#start done.
Exiting
答案 0 :(得分:0)
看起来你的测试很好,所以你的测试可能会失败,因为标题实际上没有正确设置。
我想你只需要在你的观点中添加这样的东西:
<% provide(:title, 'Title') %>
或确保您发送的标题与您正在测试的标题相同。