RSpec2不包括have_tag
测试助手。使用webrat的have_tag
或have_selector
匹配器是不可能的,因为webrat和Rails 3还不兼容。是否有一种方法来编写有用的RSpec视图测试?可以使用assert_select
代替have_tag
,但是首先可以使用Test::Unit
测试。还是不再推荐编写RSpec视图测试,因为使用Capybara或Cucumber的集成测试更好?
实际上,Webrat与Rails 3一起工作。我已经测试过了,我能够使用have_selector匹配器(have_tag不起作用)。
你可以看看这个谷歌小组讨论。基本上,你不需要webbrat。在webbrat自述中提到的配置块,并按照邮件列表解决方案,在spec_helper.rb中添加这些行:
include Webrat::Methods
include Webrat::Matchers
正如你所看到的,Webrat不再更新了,所以是的,你可能更好地与Cucumber (+ Capybara)进行集成测试。
webbrat造成了太多的麻烦,它也可以与RSpec一起使用Capybara。Capybara DSL(带有has_selector?
、has_content?
等函数)可用于以下RSpec测试:spec/requests
、spec/acceptance
或spec/integration
。
如果您使用最新版本的Capybara(~> 1.0.1) -旧版本如0.4.0不支持此功能-并将以下行添加到您的spec_helper.rb
文件
require "capybara/rspec"
require "capybara/rails"
,那么您可以编写如下RSpec请求test
require 'spec_helper'
describe "Posts" do
describe "GET /blog" do
it "should get blog posts" do
get blog_path
response.status.should be(200)
response.body.should have_selector "div#blog_header"
response.body.should have_selector "div#blog_posts"
end
end
end