Test::Unit and class attributes

A

aidy

Hi,

I have a test class the inherits from Test::Unit::TestCase.

If I create a setup and teardown method

def setup
Browser.start
log_in
end


def teardown
log_out
Browser.close
end

# and create class attributes here

@search = "Employee was created successfully"

they are not seen in my test method ...

def test_ST_MSE_4

enter_employee
click_submit
assert($ie.contains_text(@search))

end

But if I put the class attributes in the setup method, they are seen by
my test(s).
Is it normal practice to enter your class attributes in the setup
method?

Thank You

aidy
 
C

Chris Roos

Hi,

I have a test class the inherits from Test::Unit::TestCase.

If I create a setup and teardown method

def setup
Browser.start
log_in
end


def teardown
log_out
Browser.close
end

# and create class attributes here

@search = "Employee was created successfully"

they are not seen in my test method ...

def test_ST_MSE_4

enter_employee
click_submit
assert($ie.contains_text(@search))

end

But if I put the class attributes in the setup method, they are seen by
my test(s).
Is it normal practice to enter your class attributes in the setup
method?
Yup.

A class that derives from Test::Unit::TestCase is still just a normal
class. Normal classes don't follow what you seem to expect of a
test_case derived class.

Consider...

class Foo
@foo = 123
def bar
p @foo
end
end

Foo.new.bar
#=> nil

The instance of Foo that we create can't see the class instance
variable (@foo = 123).

You could change the bar method to..
p self.class.instance_variable_get:)@foo)

but I'd guess you don't want to do that.

Hope this helps,

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,180
Latest member
CryptoTax Software
Top