Take the 2-minute tour ×

Using Rails 3.2 I have a controller in a subdirectory (e.g. /controllers/data_feeds/acme_feed_controller.rb)

This controller has some constants as below

class DataFeeds::AcmeFeedController < ApplicationController


  def do_something do


In my rspec controller spec (which is in /spec/controllers/data_feeds/acme_feed_controller_spec.rb) I want to access that constant and below are two ways I've tried it (both commented out in the code below)

describe AcmeFeedController do
  if "tests something" do
    #c = AcmeFeedController.MY_CONSTANT
    #c = DataFeeds::AcmeFeedController.MY_CONSTANT

I'm clearly not understanding something about the scope in which the spec test is run. What do I need to do and equally important why (i.e. what's happening with the scopes).

Thanks for your help.

share|improve this question
Seeing the error you're getting would be helpful. –  Deefour Aug 10 '12 at 19:18

1 Answer 1

up vote 6 down vote accepted

Constants cannot be referenced with dot syntax, so DataFeeds::AcmeFeedController.MY_CONSTANT would never work in any context. You need to use :: to reference constants: DataFeeds::AcmeFeedController::MY_CONSTANT.

Note that is a ruby issue and has nothing to do with RSpec. When you face an issue like this, I recommend you figure out how to do it with plain ruby (e.g. in IRB) before worrying about how it works in RSpec (usually it will be the same, anyway).

If you want to know how constants work in ruby, I commend you watch this talk that explains them in detail.

share|improve this answer
Thanks for your help. (I realized that didn't work for me either before but that's because I reversed the module and controller name when I tried it, did the right thing with a typo I missed.) –  hershey Aug 10 '12 at 19:45

Your Answer


