Question

I have a module in app/misc/dsl/builder.rb that has this code

module Dsl
  class Builder
    def initialize(context, &block)
      return if not block_given?

      parent_context = block.binding.eval "self"
      parent_context.extend Proxy
      parent_context.object = context 

      parent_context.instance_eval &block
    end
  end

  def self.set_context(context, &block)
    Dsl::Builder.new(context, &block)
  end
end

Note: this directory misc is preloaded in application.rb

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**/}'), 
  Rails.root.join('app', 'misc', '{**/}')
]

Then, somewhere in the text (lets say at foo.rb) I have this code:

Dsl.set_context(obj) do
  #some code with obj receiving messages
end

The test stack we are using consists on Zeus+Guard+Rspec. Now, lets say I rewrite the code to something not working

Dsl.set_context(obj) do
  asdqwe #this message does not exists
end

From times to times, I receive this baffling message

  1) SomeOtherClass search_hash receiving keywords params should query for those keywords
     Failure/Error: subject.search_hash
     NoMethodError:
       undefined method `set_context' for Dsl:Module
     # ./app/misc/product_query.rb:116:in `base_search_hash'
     # ./app/misc/product_query.rb:25:in `search_hash'
     # ./spec/misc/product_query_spec.rb:78:in `block (4 levels) in <top (required)>'
     # -e:1:in `<main>'

instead of the correct message that should be regarding undefined method asdqwe

Any clue about this?

Was it helpful?

Solution

Look here

it says:

Rails 3 has been updated such that classes/modules (henceforth, C/M) are lazy loaded from the autoload paths as they are needed

so, you can do require_relative 'app/misc/dsl/builder.rb' in your rspec_helper.rb (can it be better with just require?) The problem must be that the loader doesn't know in advance where to find Dsl.set_context, but he will know once you have referenced Dsl::Builder

Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top