读安道的 ruby on rails 教程中关于 spec 测试困扰了好几天
spec_helper.rb 中
加了config.include Capybara::DSL
static_pages_spec.rb 中按照书中的代码无论如何也通不过 spec 测试,用浏览器 localhost:3000 运行没问题;后来发现在语句中加个lambda do... end
不用加config.include Capybara::DSL
居然通过了,不明觉厉啊,请高手指点下,非常感谢!
henry@henry-VirtualBox:~/rails_projects/sample_app$ bundle exec rspec spec/requests/static_pages_spec.rb
/home/henry/rails_projects/sample_app/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/henry/rails_projects/sample_app/config/application.rb to limit the frameworks that will be loaded.
...
Finished in 0.02807 seconds (files took 12.42 seconds to load)
3 examples, 0 failures
spec_helper.rb 代码如下:
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
static_pages_spec.rb 代码如下:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
lambda do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
describe "Help page" do
it "should have the content 'Help'" do
lambda do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
end
end
describe "About page" do
it "should have the content 'About Us'" do
lambda do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
end
end
end