Reputation: 2012
I am just trying to create a simple rspec method that takes an object as an argument. I can do something like the following:
def check_it(p)
before { visit p.full_path }
page.should have_title("sample title")
p.children.published.each do |pp|
check_it(pp)
end
end
so far I have not even figured out where I can put this code to avoid:
undefined method `check_it'
error, unless I put it with the other rails helpers at which point I get an
undefined method `before' for main:Object (NoMethodError)
Some help getting off the ground would be greatly appreciated.
Upvotes: 0
Views: 71
Reputation: 27779
You can add it in various places within the rspec file outside of the example. E.g.:
def check_it(p); puts p; end
describe 'foo' do
def check_it2(p); puts p; end
context 'bar' do
def check_it3(p); puts p; end
it 'does something' do
check_it 'hello'
check_it2 'world'
check_it3 'wide'
end
end
end
There are also ways to add it outside of the rspec file, e.g. put it in a file in spec/support
and include
it into the target rspec file.
Upvotes: 1
Reputation: 29308
Don't know how other people do it but I add a directory to spec and call it support because spec_helper includes this line:
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Meaning anything in the support directory will be included in each run.
So you can just place this specific method directly in a file in spec/support and it will be callable or you can include it in a module and then include that module where you need it.
Upvotes: 1