RSpec使用Rails3和RSpec2查看测试

时间:2011-08-05 16:51:49

标签: ruby-on-rails-3 rspec2 webrat

RSpec2不包含have_tag测试帮助程序。使用webrat的have_taghave_selector匹配器是不可能的,因为Webrat和Rails 3还不兼容。有没有办法编写有用的 RSpec视图测试?可以使用assert_select代替have_tag,但是可以首先进行Test::Unit次测试。或者不再推荐编写RSpec视图测试,因为与Capybara或Cucumber的集成测试更好?

2 个答案:

答案 0 :(得分:1)

实际上,Webrat使用Rails 3.我测试了这个并且我能够使用have_selector匹配器(has_tag不起作用)。

您可以查看此Google group discussion。基本上,您不需要webrat自述文件中提到的Webrat.configure块,并且按照邮件列表解决方案,在spec_helper.rb中添加以下行:

include Webrat::Methods
include Webrat::Matchers

正如您所看到的,Webrat不再那么更新,所以是的,您可能会更好地与Cucumber(+ Capybara)进行集成测试。

答案 1 :(得分:0)

Webrat造成了太多麻烦,也可以将Capybara与RSpec一起使用。 Capybara DSL(功能has_selector?has_content?等)可用于以下RSpec测试:spec/requestsspec/acceptancespec/integration。< / p>

如果你使用最新版本的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