ruby-on-rails-3 - 使用 capybara 和rails3进行Rspec View 测试

标签 ruby-on-rails-3 configuration rspec capybara matcher

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

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

我在网上看到了一些帖子,展示了如何配置 capybara 和rails3以顺利地与cucumber或rspec Controller 测试一起工作,但这并不是我真正想要的 - 也就是说,在尽可能低的级别测试 View 。

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

最佳答案

现在可以选择在测试 Controller (以及 View )时使用 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

完整代码:

引用文献:

关于ruby-on-rails-3 - 使用 capybara 和rails3进行Rspec View 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4706370/

相关文章:

ruby-on-rails - Rspec - 如何测试邮件程序是否使用了正确的模板

ruby-on-rails - 初始化后设置/更改 rails 配置值

spring-boot - 字符串列表的 Spring Boot YAML 配置

ruby-on-rails - 什么是好的 RSpec 自动化测试平台?

ruby-on-rails - 如何截断与公共(public)模式不同的所有数据(database_cleaner)

ruby-on-rails - Rails 范围 : Select Distinct Title With Values

ruby-on-rails - Rails 3.2 到 4.0 升级 : Undefined method to_datetime for false:FalseClass

java - 如何映射 URL 中包含斜线(通配符映射)

html - 使用 capybara 点击基于 ID 的链接

mysql - MySQL 上的延迟作业 : 'This version of MySQL doesn' t yet support 'LIMIT & IN/ALL/ANY/SOME subquery'