新手问题 Spec 测试的问题

hxygsh · 2015年10月28日 · 最后由 hxygsh 回复于 2015年10月31日 · 2620 次阅读

读安道的 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

没看懂问题在哪,不知道你贴的日志和代码是有问题的还是没问题的。

Rails 教程第三版已经不用 Rspec 了,建议看第三版。

没有加lambada do ... end时,虽然加了config.include Capybara::DSL 语句报错,教程说加了后者才能测试通过,可是加了报Capybada常量初始化错误,不加报visit 方法不能识别错误,加了lambada do ... end 测试通过,教程没提到这个语句,不知是教程本身有问题,还是哪里弄错了。

3 楼 已删除

lambda 没有执行吧。

嗯,果断入手第三版!!!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号