Make classes in a module available to examples

Hi

Sorry if this is documented somewhere, I can’t find it on rspec.info
or Google.

Say I have this code (simple example based no the RSpec site):
module Bank
class Account
def balance
0
end
end
end

describe “A new account” do
# what goes here?

 before do
   @account = Account.new
 end

 it "should have a balance of $0" do
   @account.balance.should == 0
 end

end

…what can I put in the describe block to make the Account class
visible? I tried include, but that doesn’t work (uninitialized
constant Account).

I don’t want to include into the global namespace, because I’ve got a
module hierarchy that could be several layers deep, and I don’t want
to flatten it out.

Thanks,
Ashley


http://www.patchspace.co.uk/

On Fri, Sep 5, 2008 at 6:25 AM, Ashley M.
[email protected] wrote:

 end

it “should have a balance of $0” do
@account.balance.should == 0
end
end

…what can I put in the describe block to make the Account class visible?
I tried include, but that doesn’t work (uninitialized constant Account).

I don’t want to include into the global namespace, because I’ve got a module
hierarchy that could be several layers deep, and I don’t want to flatten it
out.

Either fully qualify the class name in the spec:

before(:each) do
@account = Bank::Account.new
end

or put the example group in the same module

module Bank
describe “A new account” do

end
end

The RSpec codebase uses the second approach, which I personally prefer.

Pat

On 5 Sep 2008, at 15:58, Pat M. wrote:

module Bank
describe “A new account” do

end
end

The RSpec codebase uses the second approach, which I personally
prefer.

Aha, that’s good enough for me

Right now I’m fully qualifying everything, which is too verbose for my
liking

Thanks Pat
Ashley


http://www.patchspace.co.uk/

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