0
votes

I'm having an issue with using the let function provided by rspec:

In app/spec/class_spec.rb:

require 'spec_helper'
module App
   describe Class do
      let(:instance) {Class.new('param')}

      describe "#method" do
         it "does something" do
            instance.method(...)
            # clever test code
         end
      .
      .
      .
end

I've been following the RSpec book, and according to its examples (which worked through!) let should give me access to the variable instance for the rest of the 'describe Class' block. But I get the error:

Uninitialized constant App::instance

I also tried adding this code to no avail:

before :all do
    instance
end

What am I doing wrong?

1
It seems odd that you are wrapping your describe block in a module. Is this a common practice? I've never seen it before. - Peter Brown
I don't have enough experience to know what is common practice! 'Class' mentioned in the code is in the 'App' module though, so I thought I should put my test code in the same module. Also, a few examples in the reference book I've looked at do the same. - gradi3nt
I'm pretty confident that the source of that error isn't found in any of the code you've listed here. Somewhere you have code calling const_get(:instance) on App. - Myron Marston
Wait, is this a Rails model named Class? - zetetic
No rails! Believe it or not, I'm just using ruby. Class is well...just a class. - gradi3nt

1 Answers

1
votes

Instead of

module App
   describe Class do

just use:

describe App::Class

and everything should be fine hint: instead let(:instance) {Class.new('param')} you could write let(:instance) {described_class.new('param')}. It's more clever.