My ApplicationController exposes a method (e.g. sort_direction) to the
view templates by using
helper_method :sort_direction.
I then use this method in another method (e.g. sort_link) in a view
helper (application_helper.rb).
When testing the sort_link method with RSpec (in
application_helper_spec.rb) I have to stub sort_direction as the test
seems to run completely independent from the controllers (and thereby
by to the view templates exposed methods).
Unfortunately I could not find out how to stub that sort_direction
method of the controller. I always get “undefined method”.
Here is what I tried so far (inside application_helper_spec.rb):
NoMethodError:
undefined method `sort_direction’ for
#<RSpec::Core::ExampleGroup::Nested_1
This error ^^ suggests that sort_direction is being called on the
example itself rather than the helper object. Please post the spec so we
can see what’s going on.
def order_link(column, title = nil)
fetch_param(:order)
Change this ^^ to this:
helper.fetch_param(:order)
The problem is that ActionView::TestCase::Behavior, which helper specs
rely on, include the module being spec’d right in the spec, so you have
access to those methods directly.
RSpec expresses an opinion that the object should be wrapped instead by
providing a helper object that includes the module being spec’d, but
does not remove the module methods from the scope of the spec.
This error ^^ suggests that sort_direction is being called on the example itself
rather than the helper object. Please post the spec so we can see what’s going on.
Sure …
describe ApplicationHelper do
describe “order link” do
it “should create html link” do
# things I tried
view.stub(:check_param)
helper.stub(:check_param)
controller.stub(:check_param)
self.stub(:check_param)
order_link(:name)
end
end
end
Here is how check_param is called from within the helper:
module ApplicationHelper
def order_link(column, title = nil)
fetch_param(:order)
end
end
I was wrong the test fails also with the same error message:
Failure/Error: order_link(:name)
NoMethodError:
undefined method `fetch_param’ for #<#Class:0xb63d37ac:
0xb63be118>
That’s not the same error message you posted earlier:
NoMethodError:
undefined method `sort_direction’ for
#<RSpec::Core::ExampleGroup::Nested_1
This ^^ message tells us that sort_direction is not implemented on the
example. The new message (above) tells us that fetch_param is not
implemented on the helper object, which means one of two things:
fetch_param is not implemented as an instance method in the
ApplicationHelper module. This would be just a matter of implementing
it.
ApplicationHelper is not included in the helper object. It should be
included if the spec file is in spec/helpers. Is it?
Failure/Error: order_link(:name)
NoMethodError:
undefined method `fetch_param’ for #<#Class:0xb63d37ac:
0xb63be118>
That’s not the same error message you posted earlier:
NoMethodError:
undefined method `sort_direction’ for
#<RSpec::Core::ExampleGroup::Nested_1
The “#<#Class:0xb63d37ac” error shows up when using
helper.check_param in the order_link method as you suggested. The
“#<RSpec::Core::ExampleGroup::Nested_1” shows up with just using
check_param (without “helper.”).
This ^^ message tells us that sort_direction is not implemented on the example.
The new message (above) tells us that fetch_param is not implemented on the helper
object, which means one of two things:
fetch_param is not implemented as an instance method in the ApplicationHelper
module. This would be just a matter of implementing it.
It should be. It works when viewing that template in the browser.
ApplicationHelper is not included in the helper object. It should be included
if the spec file is in spec/helpers. Is it?
order_link is known and that is a method of ApplicationHelper, so I
guess it is included.
undefined local variable or method `helper’ for #<#Class:0xb66566a0:
0xb665532c>
What’s the full backtrace?
ApplicationHelper order link should create html link
Failure/Error: order_link(:name)
NoMethodError:
undefined method `fetch_param’ for #<#Class:0xb64ba0d0:
0xb64a1ef4>