【问题标题】:RSpec View testing with Rails3 and RSpec2使用 Rails3 和 RSpec2 进行 RSpec 查看测试
【发布时间】:2011-08-31 23:19:48
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    实际上,Webrat 可以与 Rails 3 配合使用。我已经对此进行了测试,并且能够使用 have_selector 匹配器(have_tag 不起作用)。

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

    include Webrat::Methods
    include Webrat::Matchers
    

    如您所见,Webrat 不再那么更新了,所以是的,使用 Cucumber (+ Capybara) 进行集成测试可能会更好。

    【讨论】:

    • 它是否只适用于那里提到的版本,'>=0.7.2.pre', :git => 'github.com/kalv/webrat.git' ?
    • @0x4a6f4672,我之前在他们的讨论中没有注意到这一点,所以我只是使用了 webrat 的常规风格。我基本上只是在我的 Gemfile 中添加了gem 'webrat'
    【解决方案2】:

    Webrat 太麻烦了,用 RSpec 使用 Capybara 也是可以的。 Capybara DSL(具有 has_selector?has_content? 等功能)可用于以下 RSpec 测试:spec/requestsspec/acceptancespec/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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多