使用 Rails3 和 RSpec2 进行 RSpec View 测试

发布于 2024-11-28 01:27:35 字数 325 浏览 0 评论 0原文

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

RSpec2 does not include an have_tag test helper. Using webrat's have_tag or have_selector matchers instead is not possible because Webrat and Rails 3 are not compatible yet. Is there a way to write useful RSpec view tests? It is possible to use assert_select instead of have_tag, but then one could Test::Unit tests in the first place. Or is it no longer recommendable to write RSpec view tests, because integration tests with Capybara or Cucumber are better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-12-05 01:27:35

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

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

include Webrat::Methods
include Webrat::Matchers

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

Actually, Webrat works with Rails 3. I have tested this and I was able to use the have_selector matcher (have_tag didn't work).

You can take a look at this Google group discussion. Basically, you don't need the Webrat.configure block mentioned in the webrat readme, and following the mailing list solution, add these lines in your spec_helper.rb:

include Webrat::Methods
include Webrat::Matchers

As you can see, Webrat is not so updated anymore, so yes, you might be better off with integration testing with Cucumber (+ Capybara).

离不开的别离 2024-12-05 01:27:35

Webrat 太麻烦了,用 Capybara 配合 RSpec 也是可以的。 Capybara DSL(具有函数 has_selector?has_content? 等)可用于以下 RSpec 测试:spec/requests、<代码>规范/验收,或规范/集成

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

Webrat caused too much trouble, it is also possible to use Capybara with RSpec. The Capybara DSL (with the functions has_selector?, has_content?, etc.) is available for the following RSpec tests: spec/requests, spec/acceptance, or spec/integration.

If you use the latest version of Capybara (~> 1.0.1) - older versions like 0.4.0 won't support this - and add the following lines to your spec_helper.rb file

require "capybara/rspec"
require "capybara/rails"

then you could write for example the following RSpec request 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文