1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
Feature: Helper specs
Helper specs are marked by `type: :helper` or if you have set
`config.infer_spec_type_from_file_location!` by placing them in `spec/helpers`.
Helper specs expose a `helper` object, which includes the helper module being
specified, the `ApplicationHelper` module (if there is one) and all of the
helpers built into Rails. It does not include the other helper modules in
your app.
To access the helper methods you're specifying, simply call them directly
on the `helper` object.
NOTE: helper methods defined in controllers are not included.
Scenario: A helper method that returns a value
Given a file named "spec/helpers/application_helper_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe ApplicationHelper, type: :helper do
describe "#page_title" do
it "returns the default title" do
expect(helper.page_title).to eq("RSpec is your friend")
end
end
end
"""
And a file named "app/helpers/application_helper.rb" with:
"""ruby
module ApplicationHelper
def page_title
"RSpec is your friend"
end
end
"""
When I run `rspec spec/helpers/application_helper_spec.rb`
Then the examples should all pass
Scenario: A helper method that accesses an instance variable
Given a file named "spec/helpers/application_helper_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe ApplicationHelper, type: :helper do
describe "#page_title" do
it "returns the instance variable" do
assign(:title, "My Title")
expect(helper.page_title).to eql("My Title")
end
end
end
"""
And a file named "app/helpers/application_helper.rb" with:
"""ruby
module ApplicationHelper
def page_title
@title || nil
end
end
"""
When I run `rspec spec/helpers/application_helper_spec.rb`
Then the examples should all pass
Scenario: Application helper is included in helper object
Given a file named "spec/helpers/widgets_helper_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe WidgetsHelper, type: :helper do
describe "#widget_title" do
it "includes the app name" do
assign(:title, "This Widget")
expect(helper.widget_title).to eq("The App: This Widget")
end
end
end
"""
And a file named "app/helpers/application_helper.rb" with:
"""ruby
module ApplicationHelper
def app_name
"The App"
end
end
"""
And a file named "app/helpers/widgets_helper.rb" with:
"""ruby
module WidgetsHelper
def widget_title
"#{app_name}: #{@title}"
end
end
"""
When I run `rspec spec/helpers/widgets_helper_spec.rb`
Then the examples should all pass
Scenario: Url helpers are defined
Given a file named "spec/helpers/widgets_helper_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe WidgetsHelper, type: :helper do
describe "#link_to_widget" do
it "links to a widget using its name" do
widget = Widget.create!(:name => "This Widget")
expect(helper.link_to_widget(widget)).to include("This Widget")
expect(helper.link_to_widget(widget)).to include(widget_path(widget))
end
end
end
"""
And a file named "app/helpers/widgets_helper.rb" with:
"""ruby
module WidgetsHelper
def link_to_widget(widget)
link_to(widget.name, widget_path(widget))
end
end
"""
When I run `rspec spec/helpers/widgets_helper_spec.rb`
Then the examples should all pass
|