The title is self explanatory.
Everything I've tried led to a "undefined method".
To clarify, I am not trying to test a helper method. I am trying to use a helper method in an integration test.
To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.
Basically helpers in Rails are used to extract complex logic out of the view so that you can organize your code better.
The describe Keyword The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.
You just need to include the relevant helper module in your test to make the methods available:
describe "foo" do
include ActionView::Helpers
it "does something with a helper method" do
# use any helper methods here
It's really as simple as that.
For anyone coming late to this question, it is answered on the Relish site.
require "spec_helper"
describe "items/search.html.haml" do
before do
controller.singleton_class.class_eval do
protected
def current_user
FactoryGirl.build_stubbed(:merchant)
end
helper_method :current_user
end
end
it "renders the not found message when @items is empty" do
render
expect(
rendered
).to match("Sorry, we can't find any items matching "".")
end
end
If you are trying to use a helper method on your view test, you can go with the following:
before do
view.extend MyHelper
end
It must be inside a describe
block.
It works for me on rails 3.2 and rspec 2.13
Based on Thomas Riboulet's post on Coderwall:
At the beginning of your spec file add this:
def helper
Helper.instance
end
class Helper
include Singleton
include ActionView::Helpers::NumberHelper
end
and then call a particular helper with helper.name_of_the_helper
.
This particular example includes the ActionView's NumberHelper. I needed the UrlHelper, so I did include ActionView::Helpers::UrlHelper
and helper.link_to
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With