RSpec2不包括have_tag测试助手。使用Webrat的have_tag或have_selector匹配器是不可能的,因为Webrat和Rails 3还不兼容。有没有一种方法可以编写有用的RSpec视图测试?可以使用assert_select而不是have_tag,但首先可以使用Test::Unit测试。还是不再推荐编写RSpec视图测试,因为与Capybara或Cucumber的集成测试更好?
发布于 2011-08-30 09:52:51
Webrat造成了太多的麻烦,也有可能与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请求测试
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发布于 2011-08-06 15:21:50
实际上,Webrat与Rails 3一起工作,我已经测试过它,并且我能够使用have_selector匹配器(have_tag没有工作)。
您可以看看这个谷歌小组讨论。基本上,您不需要webrat中提到的Webrat.configure块,在邮件列表解决方案之后,在spec_helper.rb中添加以下几行:
include Webrat::Methods
include Webrat::Matchers正如您所看到的,Webrat已经不再更新了,所以是的,您可能会更好地与Cucumber (+ Capybara)进行集成测试。
https://stackoverflow.com/questions/6959815
复制相似问题