Call a function outside the test doesn't work

Hi,

I am using Ruby 2.0 and Rspec as test framework.

Below I have two tests. One where I call get_locales function outside
the actual test (XXX) and one where I call the function inside the test
(YYY). The YYY test work but XXX could not be run due to error message
“undefined local variable or method `get_locales’”. Anyone that have
clue about this?

Thanks in advance!
Mattias

get_locales.each do |locale|
it "Test XXX " + locale do
puts locale
end
end

it "Test YYY " do
get_locales.each do |locale|
puts locale
end
end

def get_locales()
if properties[“locales”].nil?
return [“GB”, “FI”]
else
return properties[“locales”]
end
end

On Thu, Jan 2, 2014 at 11:37 AM, Mattias Andersson
[email protected] wrote:

Thanks in advance!
get_locales.each do |locale|
end
end

You are calling get_locales before defining the method. Try moving the
method definition to the top.
The test works because RSpec is probably saving the blocks and running
them after the full script.

Jesus.

Thanks for your reply, I tried to place the method in the top. But still
facing the same issue. Output: “.rb:2:in get_locales': undefined local variable or methodproperties’”

Some more ides?

Thanks
Mattias

Sorry did see first now it was another error message. But are still
stuck.

properties is probably defined within your RSpec environment as well.
You might need to rethink the layout. Shouldn’t methods like get_locales
be defined in some sort of helper module when you’re using RSpec?

yes, correct. I will have it in my helper module and have tried that as
well with error “undefined local variable”. Just presented it in a
single file format so it should be “cleaner” to describe/read my
problem.

If i define the array LOCALES_TO_TEST = [“GB”, “FI”] instead of using
the method get_locales it works (see below)… Can you see any
explanations for this?

LOCALES_TO_TEST .each do |locale|
it "Test XXX " + locale do
puts locale
end
end

Thanks for your quick reply
Mattias

Can I only get access to the methods inside “it do end”? Because it
still says “undefined local variable or method `get_locales’” when i try
to call get_locals outside “it do end”.

require ‘utils’
describe “Test” do

include Utils # get_locales is specified inside Utils

###DO NOT WORK###
get_locales.each do |locale| #
it "Test XXX " + locale do
puts locale
end
end

###WORK###
it “Test YYY " do
get_locales.each do |locale|
puts locale
load_mobile_site(”", locale)
end
end
end

I don’t think anyone can help if the only thing stated is “DO NOT WORK”

What is the error, how does it not work, what are you expecting to
happen,
etc.

Thanks for you answer, maybe have been unclear in my descriptions above.

The script cant find the methodget_locales if I place it outside the
test:

it “…” do
end

get_locales is placed in a generic test utility module. If I declare a
variable in the utility module LOCALES = [GB, SV] the script finds
LOCALES? See example below:


##Method outside the test, cant find get_locales##
get_locales.each do |locale|
it "Test XXX " + locale do
puts locale
end
end

##Variable outside the test, finds LOCALES##
LOCALES.each do |locale|
it "Test XXX " + locale do
puts locale
end
end

##Method inside the test, find get_locales##
it “Test YYY " do
get_locales.each do |locale|
puts locale
load_mobile_site(”", locale)
end
end

module Utils
LOCALES = [GB, SV]

def get_locales()
if properties[“locales”].nil?
return [“GB”, “FI”]
else
return properties[“locales”]
end
end

Thanks
Mattias

Problem solved.

Needed to move include Utils outside describe in test file and read in
the property files in get locales method.

Thanks for your help.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs