使用水豚和rails3进行Rspec视图测试

发布于 2024-10-12 21:03:20 字数 478 浏览 4 评论 0原文

我真的很喜欢 RSpec 能够分离控制器和视图测试的方式,但在让水豚匹配器在视图测试中工作时遇到一些问题。我基本上想要实现的是这样的:

describe "some page" do
  it "should render with lots of stuff" do
    assign ..
    render
    rendered.should have_button ('Any button') #or any capybara matcher, really
  end
end

我在网上看到了一些帖子,展示了如何配置水豚和rails3以顺利地与黄瓜或rspec控制器测试一起工作,但这并不是我真正想要的 - 也就是说,测试尽可能最低级别的意见。

另外,如果有另一种方法可以做到这一点(不需要大量自定义代码,因为我知道我可以编写一些匹配器,使用 nokogiri 或任何合适的工具从渲染中提取给定的选择器),那也很棒 - 不需要使用水豚。

I really like the way RSpec is able to separate controller and view tests but have some problems with getting capybara matchers to work in a view test. What i basically try to achieve is sth like this:

describe "some page" do
  it "should render with lots of stuff" do
    assign ..
    render
    rendered.should have_button ('Any button') #or any capybara matcher, really
  end
end

I've seen some posts on the net showing how to configure capybara and rails3 to work smoothly with cucumber or rspec controller tests, but this is not really what I want - that is, testing the views at the lowest level possible.

Also if there's another way to do this (not requiring lots of custom code, couse I know i could write some matchers that extract given selectors from rendered using nokogiri or whatever tool suitable) that'd be great too - using capybara is not a requirement.

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

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

发布评论

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

评论(7

倾`听者〃 2024-10-19 21:03:20

现在可以选择在测试控制器(以及视图)时使用 Capybara 匹配器(没有 Webrat 包袱)。我以这种方式使用它:

describe GlobalizeTranslationsController do

  render_views
  let(:page) { Capybara::Node::Simple.new(@response.body) }

  describe "PUT :update" do
    before do
      put :update
    end

    it "displays a flash notice" do
      page.should have_selector('p.notice')
    end

  end

end

完整代码:

参考文献:

There is now an option to use Capybara matchers (without Webrat baggage) when testing controllers (and views too). I'm using it this way:

describe GlobalizeTranslationsController do

  render_views
  let(:page) { Capybara::Node::Simple.new(@response.body) }

  describe "PUT :update" do
    before do
      put :update
    end

    it "displays a flash notice" do
      page.should have_selector('p.notice')
    end

  end

end

Full code:

References:

百思不得你姐 2024-10-19 21:03:20

Capybara 目前不支持视图规范(有计划在未来使其工作)。最简单的答案是只需将 gem 'webrat' 添加到 Gemfile 中即可。您可能没有 have_button,但您可以使用 have_selectorhave_tag 和类似的工具。

顺便说一句:据我所知,capybara 和 webrat 可以在一个项目中共存。

Capybara currently does not work with view specs (there are plans to make it work in the future). The simplest answer is to just add gem 'webrat' to the Gemfile and you're basically set. You might not have have_button but you'll have have_selector, have_tag and similar available.

Btw: as far as I know capybara and webrat can co-exist in one project.

魔法少女 2024-10-19 21:03:20

比 Pawel 的答案稍微简单一些,但要点是一样的;以下适用于 Rails 3.1.0、rspec 2.6.0、capybara 1.1.1:

page = Capybara::Node::Simple.new( rendered )
page.should have_content( "blah" )

Slightly simpler than Pawel's answer, but the gist is the same; the following works for me with rails 3.1.0, rspec 2.6.0, capybara 1.1.1:

page = Capybara::Node::Simple.new( rendered )
page.should have_content( "blah" )
凉薄对峙 2024-10-19 21:03:20

你不能在rendered上调用水豚的方法,那只是一个字符串。您可以使用 Capybara 的 string 方法 来包装 rendered在水豚节点中。然后,您可以在该节点上调用 Capybara 的方法:

describe "some page" do
  it "should render with lots of stuff" do
    assign ..
    render
    Capybara.string(rendered).should have_button('Any button')
  end
end

有关更多信息,请查看这篇文章:

http://www.tamingthemindmonkey.com/2011/11/07/capybara-matchers-and-scoping-in-view-specs

You can't call capybara's methods on rendered, that's just a string. You can use Capybara's string method though to wrap rendered in a Capybara node. Then, you can call Capybara's methods on that node:

describe "some page" do
  it "should render with lots of stuff" do
    assign ..
    render
    Capybara.string(rendered).should have_button('Any button')
  end
end

For more information, check out this post:

http://www.tamingthemindmonkey.com/2011/11/07/capybara-matchers-and-scoping-in-view-specs

困倦 2024-10-19 21:03:20

在此页面底部的“Webrat 和 Capybara”部分中,rspec 视图规范似乎不支持 Capybara

http://relishapp.com/rspec/rspec-rails

At the bottom of this page, in the "Webrat and Capybara" section, it looks like Capybara is unsupported for rspec view specs

http://relishapp.com/rspec/rspec-rails

烟燃烟灭 2024-10-19 21:03:20

更新这个旧问题,因为自从添加了大多数其他答案以来,事情已经发生了变化:

Capybara 现在确实支持视图规范(开箱即用),这记录在 水豚的主分支

引用文档:

最后,视图规范支持 Capybara 匹配器:

RSpec.describe "todos/show.html.erb", type: :view do
  it "displays the todo title" do
    assign :todo, Todo.new(title: "Buy milk")

    render

    expect(rendered).to have_css("header h1", text: "Buy milk")
  end
end

对这些匹配器的支持似乎已在早期版本中添加,而无需额外的 let(:page) 样式代码。 (它在水豚 2.4.4 和 2.2 中对我有用)。
另请注意,仅支持有限的匹配器子集;但是,您可以通过使用 Capybara.string 获得更多功能;前任:

expect(Capybara.string(rendered).first('td')).to have_no_content 'Tom Riddle'

Updating this old question as things have changed since most of the other answers were added:

Capybara now does support view specs (out of the box) and this is documented on Capybara's master branch.

To quote the docs:

Finally, Capybara matchers are supported in view specs:

RSpec.describe "todos/show.html.erb", type: :view do
  it "displays the todo title" do
    assign :todo, Todo.new(title: "Buy milk")

    render

    expect(rendered).to have_css("header h1", text: "Buy milk")
  end
end

Support for these without additional let(:page) style code appears to have been added in an earlier version. (It's working for me in capybara 2.4.4 and 2.2).
Note also that only a limited subset of matchers are supported; however you can gain more functionality by using Capybara.string; ex:

expect(Capybara.string(rendered).first('td')).to have_no_content 'Tom Riddle'
我一向站在原地 2024-10-19 21:03:20

您还可以使用水豚语法

describe "some page" do 
  it 'should render hello with name' do
    assign(:user, double("User", first_name: "John"))
    render
    expect(rendered).to have_content("Hello John")
  end
end

You can also use capybara syntax

describe "some page" do 
  it 'should render hello with name' do
    assign(:user, double("User", first_name: "John"))
    render
    expect(rendered).to have_content("Hello John")
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文