Reputation: 6029
I'm writing a test for some existing Ruby code which looks something like
module Foo
module Bar
private
def function_i_want_to_test (a, b)
if controller.is_something? a, b
puts a
else
puts b
end
end
end
end
The varaible controller
magically inserted into the function from somewhere else. To access this function from my tests, I can do include Foo::Bar
in my test class and call the function normally afterwards. Unfortunately this doesn't work as controller
isn't set. Any ideas how I could set the controller
variable from my test?
Upvotes: 0
Views: 164
Reputation: 54593
The module seems to assume "controller" is available in the class/module it's being included into. Perhaps it's for inclusion in Rails ActionController classes? Also, it's probably meant to be a method call, not a local variable. So, do something like this:
class MyClass
include Foo::Bar
def controller
# Do stuff.
end
end
For a test, you can also use Mocha (or other mocking libs) to make the process a little less verbose.
def test_my_thing
thing = Object.new
thing.extend(Foo::Bar)
thing.expects(:controller).returns("whatever")
end
Upvotes: 1