ruby-on-rails - 使用 Ruby on Rails 和 RSpec 为 helper 编写规范

标签 ruby-on-rails ruby rspec bdd

我一直在为 Controller 和模型编写规范,但我从未编写过辅助规范。我不知道从哪里开始。

我在 application_helper.rb 中有以下片段

  def title(page_title)
    content_for(:title) { page_title }
  end
  • 我应该如何编写代码的辅助规范?
  • 此外,如果有任何开源 Rails 应用程序可以显示良好的辅助测试/规范,请告诉我。

最佳答案

来自rspec-rails docs on Helper Specs :

Helper specs live in spec/helpers, and mix in ActionView::TestCase::Behavior.

Provides a helper object which mixes in the helper module being spec'd, along with ApplicationHelper (if present).

require 'spec_helper'
describe ApplicationHelper do
  describe "#title" do
    it "displays the title" do
      # helper is an instance of ActionView::Base configured with the
      # ApplicationHelper and all of Rails' built-in helpers
      expect(helper.title).to match /Some Title/
    end
  end 
end

关于ruby-on-rails - 使用 Ruby on Rails 和 RSpec 为 helper 编写规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1996757/

相关文章:

ruby-on-rails - RVM 没有在 `fish` 中设置我的 PATH,但应该是

ruby-on-rails - 通过关联定义自定义 ActiveRecord 创建者

ruby-on-rails - 防止 rspec 输出服务器和数据库日志

ruby - RSpec Rake 文件,但没有要加载的此类文件 -- rake/tasklib

ruby - 使用 rspec 仅打印失败示例的数量

javascript - 如何对本身就是 URL 的 URL 参数进行 URL 编码?

ruby-on-rails - 文字和构造函数之间的区别? ([] 与 Array.new 和 {} 与 Hash.new)

ruby-on-rails - JBuilder中未定义的局部变量或方法 `json'

ruby-on-rails - CarrierWave:为所有版本化文件创建相同、唯一的文件名

css - Font Awesome 图标不显示