I am trying to access instance variables I have defined in RSpec in the modules that I am mixing into RSpec, but I can't seem to get this to work.
A simplifed spec file shows the problem I am having:
my_spec.rb
require 'rspec'
describe 'passing instance variables from specs into ruby mixins' do
it 'should pass the instance variable to the module' do
@a = 'a'
A.a.should == 'a'
end
it 'should pass the instance variable to the module in the module' do
@b = 'b'
A.b.should == 'b'
end
it 'should pass instance varisables from one module to the other' do
A.c.should == 'c'
end
end
module B
def b
return @b
end
def c
return @c
end
end
module A
extend B
@c = 'c'
def self.a
return @a
end
end
Results:
1) passing instance variables from specs into ruby mixins should pass the instance variable to the module
Failure/Error: A.a.should == 'a'
expected: "a"
got: nil (using ==)
# ./spec/my_spec.rb:6:in `block (2 levels) in <top (required)>'
2) passing instance variables from specs into ruby mixins should pass the instance variable to the module in the module
Failure/Error: A.b.should == 'b'
expected: "b"
got: nil (using ==)
# ./spec/my_spec.rb:11:in `block (2 levels) in <top (required)>'
Basically, I want to be able to access the instance variables @a, @b in both the modules A and B. I have tried using class variables @@a and @@b, but this doesn't work.
I can use global variables ($a and $b), and this works, but I feel this isn't elegant as they're global variables, which are evil.
Working code:
require 'rspec'
describe 'passing instance variables from specs into ruby mixins' do
it 'should pass the instance variable to the module' do
$a = 'a'
A.a.should == 'a'
end
it 'should pass the instance variable to the module in the module' do
$b = 'b'
A.b.should == 'b'
end
it 'should pass instance varisables from one module to the other' do
A.c.should == 'c'
end
end
module B
def b
return $b
end
def c
return @c
end
end
module A
extend B
@c = 'c'
def self.a
return $a
end
end
Any ideas?